Yayson definitions (#11484)

* Add yayson Store typings

* Add yayson Store tests

* Improve yayson coverage

* Specify version in yayson definition

* Remove yayson generics and tidy
This commit is contained in:
David Wood
2016-09-25 16:21:10 +01:00
committed by Andy
parent 989c39d1ba
commit 3ab295950c
2 changed files with 180 additions and 0 deletions

116
yayson/yayson-tests.ts Normal file
View File

@@ -0,0 +1,116 @@
/// <reference path="yayson.d.ts" />
import * as Yayson from "yayson";
const yayson = Yayson({ adapter: 'default' }) || Yayson({ adapter: 'sequelize' }) || Yayson();
const Adapter = yayson.Adapter;
const Store = yayson.Store;
const Presenter = yayson.Presenter;
interface Thing {
hello: 'world' | 'you';
}
function test_store() {
const store = new Store();
store.sync('some string').toString();
store.sync(1234).toString();
store.sync({ any: 'thing' }).toString();
store.find('mytype', '1234').toString();
<[]> store.findAll('mytype');
store.remove('mytype', '1234');
store.remove('mytype');
store.reset();
<Yayson.Record[]> store.records;
store.records[0].attributes.toString();
store.records[0].id.toString();
store.records[0].relationships.toString();
<string> store.records[0].type;
store.relations['something'];
}
function test_presenter_static() {
Presenter.render('something').toString();
Presenter.render({}).toString();
Presenter.render({}, { meta: {} }).toString();
<[]> Presenter.render([]);
<[]> Presenter.render([], { meta: {} });
const promiseNum: PromiseLike<number> = null;
Presenter.render(promiseNum).then(data => data.toExponential());
Presenter.render(promiseNum, { meta: { so: 'meta' } }).then(data => data.toExponential());
Presenter.toJSON({ any: 'thing' }).toString();
Presenter.toJSON({ other: 'thing' }, { meta: { so: 'meta' } }).toString();
}
function test_presenter_instance() {
const presenter = new Presenter();
presenter.render({}).toString();
presenter.render({}, { meta: {} }).toString();
<[]> presenter.render([]);
<[]> presenter.render([], { meta: {} });
const promiseNum: PromiseLike<number> = null;
presenter.render(promiseNum).then(data => data.toExponential());
presenter.render(promiseNum, { meta: { so: 'meta' } }).then(data => data.toExponential());
presenter.toJSON({ any: 'thing' }).toString();
presenter.toJSON({ other: 'thing' }, { meta: { so: 'meta' } }).toString();
}
function test_presenter_inheritance() {
class MotorPresenter extends Presenter {
type = 'motors';
relationships() {
return { car: CarPresenter }
};
selfLinks(instance: any) {
return '/cars/' + this.id(instance);
};
}
class CarPresenter extends Presenter {
type = 'cars'
relationships() {
return { motor: MotorPresenter }
};
}
const motor: { id: any; car: any } = {
id: 2,
car: null
}
const car = {
id: 1,
motor: motor
}
motor.car = car;
CarPresenter.toJSON(car).toString();
CarPresenter.toJSON(car, { meta: { so: 'meta' } }).toString();
CarPresenter.render({ id: 3 }).toString();
CarPresenter.render({ id: 3 }, { meta: { so: 'meta' } }).toString();
CarPresenter.render({ id: 3 }, { car: { id: 3 } }).toString();
}
function test_adapter() {
Adapter.get({ name: 'Abraham' }).toString();
Adapter.get({ name: 'Abraham' }, 'name').toString();
<string> Adapter.id({ id: 5 });
}

64
yayson/yayson.d.ts vendored Normal file
View File

@@ -0,0 +1,64 @@
// Type definitions for yayson v2.0.1
// Project: https://github.com/confetti/yayson
// Definitions by: David Wood <https://github.com/Codesleuth>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module 'yayson' {
class Store {
sync(obj: {}): any;
find(type: string, id: string): any;
findAll(type: string): any[];
remove(type: string, id?: string): void;
reset(): void;
records: y.Record[];
relations: { [key: string]: any };
}
interface Adapter {
get(model: {}, key?: string): any;
id(model: {}): string;
}
class Presenter {
static adapter: string;
static render<T>(instanceOrCollection: PromiseLike<T>, options?: y.JsonOptions): PromiseLike<T>;
static render(instanceOrCollection: {}, options?: y.JsonOptions): any;
static toJSON(instanceOrCollection: {}, options?: y.JsonOptions): any;
render<T>(instanceOrCollection: PromiseLike<T>, options?: y.JsonOptions): PromiseLike<T>;
render(instanceOrCollection: {}, options?: y.JsonOptions): any;
toJSON(instanceOrCollection: {}, options?: y.JsonOptions): any;
id(instance: {}): string;
type: string;
}
interface Yayson {
Store: typeof Store;
Presenter: typeof Presenter;
Adapter: Adapter;
}
interface YaysonOptions {
adapter?: 'default' | 'sequelize';
}
function y(arg?: YaysonOptions): Yayson;
namespace y {
interface JsonOptions {
[key: string]: any;
meta?: {};
}
interface Record {
id: any;
type: string;
attributes: any;
relationships: any;
}
}
export = y;
}