Skip to content

Commit

Permalink
fix type definitions #1
Browse files Browse the repository at this point in the history
  • Loading branch information
andersondanilo committed May 3, 2020
1 parent 689d644 commit b06cc3a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
{
"name": "jsonapi-fractal",
"version": "1.0.13",
"version": "1.0.16",
"description": "JSON:API Serializer inspired by Fractal (PHP)",
"license": "MIT",
"main": "dist/index",
"types": "./dist/index.d.ts",
"module": "./dist-esm/index.js",
"exports": {
".": {
"require": "./dist/index.js",
"default": "./dist-esm/index.js"
}
},
"scripts": {
"test": "jest",
"build": "tsc && tsc -m es6 --outDir dist-esm",
"build": "tsc",
"lint": "tslint src/**/*.ts tests/*.ts"
},
"jest": {
Expand Down
5 changes: 5 additions & 0 deletions src/JsonApiResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default interface JsonApiResponse {
data: any;
meta?: any;
included?: any[];
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Context from './Context'
import JsonApiError from './errors/JsonApiError'
import DefaultTransformer from './DefaultTransformer'
import deserialize from './deserializer'
import JsonApiResponse from './JsonApiResponse'
import { serializeContext, serialize, transform } from './serializer'
import { whitelist } from './utils'

export { Transformer, DefaultTransformer, transform, serialize, deserialize, whitelist }
export { Transformer, DefaultTransformer, transform, serialize, JsonApiResponse, deserialize, whitelist, Options }
11 changes: 6 additions & 5 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import Transformer from './Transformer'
import JsonApiError from './errors/JsonApiError'
import Options from './Options'
import { whitelist, changeCase } from './utils'
import JsonApiResponse from './JsonApiResponse'

export function transform () {
export function transform (): Context {
return new Context(serializeContext);
}

export function serialize (data, type, options) {
export function serialize (data, type, options): JsonApiResponse {
if (!options) {
options = {}
}
Expand All @@ -21,7 +22,7 @@ export function serialize (data, type, options) {
.serialize()
}

export function serializeContext (ctx: Context) {
export function serializeContext (ctx: Context): JsonApiResponse {
if (!ctx.options) {
ctx.options = {};
}
Expand Down Expand Up @@ -53,7 +54,7 @@ export function serializeContext (ctx: Context) {
return result
}

function serializeEntity (entity, transformer: Transformer, options: Options, includedByType) {
function serializeEntity (entity, transformer: Transformer, options: Options, includedByType): any {
let attributes = { ...transformer.transform(entity, options) };
const idKey = options.idKey || 'id';
const id = attributes[idKey] || entity[idKey];
Expand Down Expand Up @@ -102,7 +103,7 @@ function serializeEntity (entity, transformer: Transformer, options: Options, in
return data
}

function serializeRelation (entity, transformer: Transformer, options: Options, included: boolean, includedByType) {
function serializeRelation (entity, transformer: Transformer, options: Options, included: boolean, includedByType) : any {
if (!entity) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/included-transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('transform', () => {

const entitySerialized = transform()
.withInput(entity)
.withTransformer(new UserTransformer)
.withTransformer(new UserTransformer())
.withOptions({ idKey: '_id' })
.serialize();

Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": [
"tslint:latest",
"tslint-config-prettier"
"tslint-config-prettier",
"tslint:recommended"
]
}

0 comments on commit b06cc3a

Please sign in to comment.