mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 12:30:18 +00:00
Add types for jsonapi-serializer
https://github.com/SeyZ/jsonapi-serializer/issues/136
This commit is contained in:
Vendored
+111
@@ -0,0 +1,111 @@
|
||||
// Type definitions for jsonapi-serializer 3.6
|
||||
// Project: https://github.com/SeyZ/jsonapi-serializer#readme
|
||||
// Definitions by: Frank Chiang <https://github.com/chiangf>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
export type RefFunction = (current: any, item: any) => string;
|
||||
|
||||
export interface Relation {
|
||||
ref: string | RefFunction;
|
||||
attributes?: string[];
|
||||
included?: boolean;
|
||||
}
|
||||
|
||||
export interface SerializerOptions {
|
||||
ref?: (() => void) | boolean | string;
|
||||
included?: boolean;
|
||||
id?: string;
|
||||
attributes?: string[];
|
||||
topLevelLinks?: string[] | Array<() => void>;
|
||||
dataLinks?: string[] | Array<() => void>;
|
||||
dataMeta?: (() => void) | object;
|
||||
relationshipLinks?: object;
|
||||
relationshipMeta?: object;
|
||||
ignoreRelationshipData?: boolean;
|
||||
keyForAttribute?: string | KeyForAttribute;
|
||||
nullIfMissing?: boolean;
|
||||
pluralizeType?: boolean;
|
||||
typeForAttribute?: TypeForAttribute;
|
||||
meta?: object;
|
||||
transform?: (() => void);
|
||||
}
|
||||
|
||||
export interface KeyForAttribute {
|
||||
(attribute: string): string;
|
||||
}
|
||||
|
||||
export interface TypeForAttribute {
|
||||
(attribute: string, object?: any): any;
|
||||
}
|
||||
|
||||
export interface Transform {
|
||||
(record: any): any;
|
||||
}
|
||||
|
||||
export interface DeserializerOptions {
|
||||
id?: string;
|
||||
keyForAttribute?:
|
||||
| "dash-case"
|
||||
| "lisp-case"
|
||||
| "spinal-case"
|
||||
| "kebab-case"
|
||||
| "underscore_case"
|
||||
| "snake_case"
|
||||
| "camelCase"
|
||||
| "CamelCase";
|
||||
pluralizeType?: boolean;
|
||||
typeAsAttribute?: boolean;
|
||||
transform?: Transform;
|
||||
}
|
||||
|
||||
export interface DeserializerConstructor {
|
||||
new (opts: DeserializerOptions): Deserializer;
|
||||
}
|
||||
|
||||
export interface SerializerConstructor {
|
||||
new (collectionName: string, opts: SerializerOptions): Serializer;
|
||||
}
|
||||
|
||||
export interface ErrorConstructor {
|
||||
new (opts: JSONAPIErrorOptions | JSONAPIErrorOptions[]): JSONAPIError;
|
||||
}
|
||||
|
||||
export interface JSONAPIError {
|
||||
errors: any[];
|
||||
}
|
||||
|
||||
export interface JSONAPIErrorOptions {
|
||||
id?: string;
|
||||
status?: string;
|
||||
code?: string;
|
||||
title?: string;
|
||||
detail?: string;
|
||||
source?: {
|
||||
pointer?: string;
|
||||
parameter?: string;
|
||||
};
|
||||
links?: {
|
||||
about?: string;
|
||||
};
|
||||
meta?: any;
|
||||
}
|
||||
|
||||
export interface Serializer {
|
||||
serialize(data: any): any;
|
||||
}
|
||||
|
||||
export interface Deserializer {
|
||||
deserialize(data: any, callback: Callback): void;
|
||||
|
||||
deserialize(data: any): Promise<any>;
|
||||
}
|
||||
|
||||
export interface Callback {
|
||||
(error: Error, response: any): any;
|
||||
}
|
||||
|
||||
export let Serializer: SerializerConstructor;
|
||||
export let Deserializer: DeserializerConstructor;
|
||||
export let JSONAPIError: ErrorConstructor;
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Serializer, Deserializer, JSONAPIError } from "jsonapi-serializer";
|
||||
|
||||
declare let firstName: string;
|
||||
declare let lastName: string;
|
||||
|
||||
const UserSerializer = new Serializer("users", {
|
||||
id: "id",
|
||||
attributes: ["firstName", "lastName"],
|
||||
keyForAttribute: "camelCase",
|
||||
pluralizeType: false
|
||||
});
|
||||
|
||||
const users = UserSerializer.serialize({ firstName, lastName });
|
||||
|
||||
const UserDeserializer = new Deserializer({
|
||||
id: "id",
|
||||
keyForAttribute: "camelCase",
|
||||
pluralizeType: false,
|
||||
typeAsAttribute: true
|
||||
});
|
||||
|
||||
UserDeserializer.deserialize(users);
|
||||
|
||||
new JSONAPIError({
|
||||
code: "_code",
|
||||
detail: "_detail",
|
||||
id: "_id",
|
||||
links: {
|
||||
about: "_about"
|
||||
},
|
||||
source: {
|
||||
parameter: "_parameter",
|
||||
pointer: "_pointer"
|
||||
},
|
||||
status: "_status",
|
||||
title: "_title"
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": ["es6"],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": ["../"],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": ["index.d.ts", "jsonapi-serializer-tests.ts"]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user