diff --git a/docs/README.md b/docs/README.md index 87d8547..c1ff960 100644 --- a/docs/README.md +++ b/docs/README.md @@ -23,6 +23,7 @@ jsonapi-fractal - [AttributesObject](README.md#attributesobject) - [Context](README.md#context) +- [DeserializeOptions](README.md#deserializeoptions) - [DocumentObject](README.md#documentobject) - [ExistingDocumentObject](README.md#existingdocumentobject) - [ExistingResourceObject](README.md#existingresourceobject) @@ -39,6 +40,10 @@ jsonapi-fractal - [SerializeOptions](README.md#serializeoptions) - [TransformerRelationships](README.md#transformerrelationships) +### Variables + +- [typeField](README.md#typefield) + ### Functions - [deserialize](README.md#deserialize) @@ -58,9 +63,9 @@ This type can be useful to enforce some input to be JSON-compatible or as a supe #### Defined in -node_modules/type-fest/source/basic.d.ts:22 +node_modules/type-fest/source/basic.d.ts:45 -___ +--- ## Other Type Aliases @@ -70,9 +75,9 @@ ___ #### Defined in -[src/types.ts:17](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L17) +[src/types.ts:17](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L17) -___ +--- ### Context @@ -80,25 +85,41 @@ ___ #### Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `TEntity` | +| Name | Type | +| :----------------- | :-------- | +| `TEntity` | `TEntity` | | `TExtraProperties` | `unknown` | #### Type declaration -| Name | Type | -| :------ | :------ | -| `included` | `boolean` | -| `input` | `TEntity` \| `undefined` | -| `options` | [`Options`](README.md#options)<`TExtraProperties`\> | +| Name | Type | +| :------------ | :---------------------------------------------------------------------- | +| `included` | `boolean` | +| `input` | `TEntity` \| `undefined` | +| `options` | [`Options`](README.md#options)<`TExtraProperties`\> | | `transformer` | [`Transformer`](classes/Transformer.md)<`TEntity`, `TExtraProperties`\> | #### Defined in -[src/context.ts:5](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L5) +[src/context.ts:5](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L5) + +--- + +### DeserializeOptions + +Ƭ **DeserializeOptions**<`TExtraOptions`\>: [`Options`](README.md#options)<`TExtraOptions`\> & { `injectType?`: `boolean` } -___ +#### Type parameters + +| Name | Type | +| :-------------- | :----- | +| `TExtraOptions` | `void` | + +#### Defined in + +[src/types.ts:64](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L64) + +--- ### DocumentObject @@ -106,9 +127,9 @@ ___ #### Defined in -[src/types.ts:15](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L15) +[src/types.ts:15](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L15) -___ +--- ### ExistingDocumentObject @@ -116,19 +137,19 @@ ___ #### Defined in -[src/types.ts:11](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L11) +[src/types.ts:11](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L11) -___ +--- ### ExistingResourceObject -Ƭ **ExistingResourceObject**: [`ResourceIdentifierObject`](README.md#resourceidentifierobject) & { `attributes`: [`AttributesObject`](README.md#attributesobject) ; `id`: `string` ; `links?`: [`LinkObject`](README.md#linkobject) ; `relationships?`: `Record`<`string`, [`RelationshipObject`](README.md#relationshipobject)\> } +Ƭ **ExistingResourceObject**: [`ResourceIdentifierObject`](README.md#resourceidentifierobject) & { `attributes`: [`AttributesObject`](README.md#attributesobject) ; `id`: `string` ; `links?`: [`LinkObject`](README.md#linkobject) ; `relationships?`: `Record`<`string`, [`RelationshipObject`](README.md#relationshipobject)\> } #### Defined in -[src/types.ts:28](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L28) +[src/types.ts:28](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L28) -___ +--- ### LinkObject @@ -136,9 +157,9 @@ ___ #### Defined in -[src/types.ts:21](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L21) +[src/types.ts:21](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L21) -___ +--- ### MetaObject @@ -146,9 +167,9 @@ ___ #### Defined in -[src/types.ts:19](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L19) +[src/types.ts:19](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L19) -___ +--- ### NewDocumentObject @@ -156,19 +177,19 @@ ___ #### Defined in -[src/types.ts:13](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L13) +[src/types.ts:13](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L13) -___ +--- ### NewResourceObject -Ƭ **NewResourceObject**: `Omit`<[`ExistingResourceObject`](README.md#existingresourceobject), ``"id"``\> +Ƭ **NewResourceObject**: `Omit`<[`ExistingResourceObject`](README.md#existingresourceobject), `"id"`\> #### Defined in -[src/types.ts:35](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L35) +[src/types.ts:35](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L35) -___ +--- ### Options @@ -176,25 +197,25 @@ ___ #### Type parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :-------------- | :----- | | `TExtraOptions` | `void` | #### Type declaration -| Name | Type | Description | -| :------ | :------ | :------ | -| `changeCase?` | [`CaseType`](enums/CaseType.md) | change case of the attribute keys | -| `changeCaseDeep?` | `boolean` | if true, also apply the change for sub objects | -| `extra?` | `TExtraOptions` | custom properties, that are available in the transformer | -| `fields?` | `Record`<`string`, `string`[]\> | map of EntityType => Fields Array, e.g. `{"users": ["name", "age"], "images": ["width"]}` | -| `idKey?` | `string` | key that should be used as the id | +| Name | Type | Description | +| :---------------- | :------------------------------ | :---------------------------------------------------------------------------------------- | +| `changeCase?` | [`CaseType`](enums/CaseType.md) | change case of the attribute keys | +| `changeCaseDeep?` | `boolean` | if true, also apply the change for sub objects | +| `extra?` | `TExtraOptions` | custom properties, that are available in the transformer | +| `fields?` | `Record`<`string`, `string`[]\> | map of EntityType => Fields Array, e.g. `{"users": ["name", "age"], "images": ["width"]}` | +| `idKey?` | `string` | key that should be used as the id | #### Defined in -[src/types.ts:50](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L50) +[src/types.ts:50](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L50) -___ +--- ### RelationshipObject @@ -202,16 +223,16 @@ ___ #### Type declaration -| Name | Type | -| :------ | :------ | -| `data` | [`ResourceIdentifierObject`](README.md#resourceidentifierobject) \| [`ResourceIdentifierObject`](README.md#resourceidentifierobject)[] | -| `links?` | [`LinkObject`](README.md#linkobject) | +| Name | Type | +| :------- | :------------------------------------------------------------------------------------------------------------------------------------- | +| `data` | [`ResourceIdentifierObject`](README.md#resourceidentifierobject) \| [`ResourceIdentifierObject`](README.md#resourceidentifierobject)[] | +| `links?` | [`LinkObject`](README.md#linkobject) | #### Defined in -[src/types.ts:39](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L39) +[src/types.ts:39](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L39) -___ +--- ### RelationshipTransformerInfo @@ -219,24 +240,24 @@ ___ #### Type parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :-------------- | :-------------- | | `TExtraOptions` | `TExtraOptions` | -| `T` | `unknown` | +| `T` | `unknown` | #### Type declaration -| Name | Type | -| :------ | :------ | -| `included` | `boolean` | -| `input` | `T` | +| Name | Type | +| :------------ | :------------------------------------------------------------- | +| `included` | `boolean` | +| `input` | `T` | | `transformer` | [`Transformer`](classes/Transformer.md)<`T`, `TExtraOptions`\> | #### Defined in -[src/transformer.ts:24](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L24) +[src/transformer.ts:24](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L24) -___ +--- ### RelationshipTransformerInfoFunction @@ -244,9 +265,9 @@ ___ #### Type parameters -| Name | -| :------ | -| `TEntity` | +| Name | +| :-------------- | +| `TEntity` | | `TExtraOptions` | #### Type declaration @@ -255,9 +276,9 @@ ___ ##### Parameters -| Name | Type | -| :------ | :------ | -| `entity` | `TEntity` | +| Name | Type | +| :-------- | :----------------------------------------------- | +| `entity` | `TEntity` | | `options` | [`Options`](README.md#options)<`TExtraOptions`\> | ##### Returns @@ -266,9 +287,9 @@ ___ #### Defined in -[src/transformer.ts:19](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L19) +[src/transformer.ts:19](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L19) -___ +--- ### ResourceIdentifierObject @@ -276,16 +297,16 @@ ___ #### Type declaration -| Name | Type | -| :------ | :------ | -| `id` | `string` | +| Name | Type | +| :----- | :------- | +| `id` | `string` | | `type` | `string` | #### Defined in -[src/types.ts:23](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L23) +[src/types.ts:23](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L23) -___ +--- ### ResourceObject @@ -293,25 +314,25 @@ ___ #### Defined in -[src/types.ts:37](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L37) +[src/types.ts:37](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L37) -___ +--- ### SerializeOptions -Ƭ **SerializeOptions**<`TExtraOptions`\>: [`Options`](README.md#options)<`TExtraOptions`\> & { `included?`: `boolean` ; `relationships?`: `string`[] \| `Record`<`string`, `string`\> } +Ƭ **SerializeOptions**<`TExtraOptions`\>: [`Options`](README.md#options)<`TExtraOptions`\> & { `included?`: `boolean` ; `relationships?`: `string`[] \| `Record`<`string`, `string`\> } #### Type parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :-------------- | :----- | | `TExtraOptions` | `void` | #### Defined in -[src/types.ts:63](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/types.ts#L63) +[src/types.ts:69](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/types.ts#L69) -___ +--- ### TransformerRelationships @@ -319,14 +340,24 @@ ___ #### Type parameters -| Name | -| :------ | -| `TEntity` | +| Name | +| :-------------- | +| `TEntity` | | `TExtraOptions` | #### Defined in -[src/transformer.ts:14](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L14) +[src/transformer.ts:14](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L14) + +## Variables + +### typeField + +• `Const` **typeField**: typeof [`typeField`](README.md#typefield) + +#### Defined in + +[src/deserializer.ts:29](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/deserializer.ts#L29) ## Functions @@ -338,17 +369,17 @@ Deserialize a JSON:API response #### Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `TEntity` | +| Name | Type | +| :-------------- | :-------- | +| `TEntity` | `TEntity` | | `TExtraOptions` | `unknown` | #### Parameters -| Name | Type | -| :------ | :------ | -| `response` | [`DocumentObject`](README.md#documentobject) | -| `options` | [`Options`](README.md#options)<`TExtraOptions`\> | +| Name | Type | +| :--------- | :--------------------------------------------------------------------- | +| `response` | [`DocumentObject`](README.md#documentobject) | +| `options` | [`DeserializeOptions`](README.md#deserializeoptions)<`TExtraOptions`\> | #### Returns @@ -356,9 +387,9 @@ Deserialize a JSON:API response #### Defined in -[src/deserializer.ts:12](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/deserializer.ts#L12) +[src/deserializer.ts:12](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/deserializer.ts#L12) -___ +--- ### serialize @@ -368,17 +399,17 @@ Serialize the entity #### Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `TEntity` | +| Name | Type | +| :-------------- | :-------- | +| `TEntity` | `TEntity` | | `TExtraOptions` | `unknown` | #### Parameters -| Name | Type | Description | -| :------ | :------ | :------ | -| `data` | `TEntity` | entity to be serialized | -| `type` | `string` | type of the entity | +| Name | Type | Description | +| :--------- | :----------------------------------------------------------------- | :-------------------------------- | +| `data` | `TEntity` | entity to be serialized | +| `type` | `string` | type of the entity | | `options?` | [`SerializeOptions`](README.md#serializeoptions)<`TExtraOptions`\> | options used in the serialization | #### Returns @@ -387,9 +418,9 @@ Serialize the entity #### Defined in -[src/serializer.ts:32](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/serializer.ts#L32) +[src/serializer.ts:32](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/serializer.ts#L32) -___ +--- ### transform @@ -399,9 +430,9 @@ Create a ContextBuilder, used to configure the transformation #### Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `TEntity` | +| Name | Type | +| :-------------- | :-------- | +| `TEntity` | `TEntity` | | `TExtraOptions` | `unknown` | #### Returns @@ -410,9 +441,9 @@ Create a ContextBuilder, used to configure the transformation #### Defined in -[src/serializer.ts:21](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/serializer.ts#L21) +[src/serializer.ts:21](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/serializer.ts#L21) -___ +--- ### whitelist @@ -422,10 +453,10 @@ Keep only a set of fields on a given object #### Parameters -| Name | Type | -| :------ | :------ | -| `object` | `unknown` | -| `list` | `string`[] | +| Name | Type | +| :------- | :--------- | +| `object` | `unknown` | +| `list` | `string`[] | #### Returns @@ -433,4 +464,4 @@ Keep only a set of fields on a given object #### Defined in -[src/utils.ts:54](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/utils.ts#L54) +[src/utils.ts:54](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/utils.ts#L54) diff --git a/docs/classes/ContextBuilder.md b/docs/classes/ContextBuilder.md index ab9421f..42f7ee5 100644 --- a/docs/classes/ContextBuilder.md +++ b/docs/classes/ContextBuilder.md @@ -4,9 +4,9 @@ ## Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `TEntity` | +| Name | Type | +| :----------------- | :-------- | +| `TEntity` | `TEntity` | | `TExtraProperties` | `unknown` | ## Table of contents @@ -40,20 +40,20 @@ #### Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `TEntity` | +| Name | Type | +| :----------------- | :-------- | +| `TEntity` | `TEntity` | | `TExtraProperties` | `unknown` | #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :--------------- | :-------------------------------------------------------------------------------------------------------------------------- | | `renderFunction` | (`c`: [`Context`](../README.md#context)<`TEntity`, `TExtraProperties`\>) => [`DocumentObject`](../README.md#documentobject) | #### Defined in -[src/context.ts:18](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L18) +[src/context.ts:18](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L18) ## Properties @@ -63,9 +63,9 @@ #### Defined in -[src/context.ts:15](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L15) +[src/context.ts:15](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L15) -___ +--- ### input @@ -73,9 +73,9 @@ ___ #### Defined in -[src/context.ts:13](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L13) +[src/context.ts:13](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L13) -___ +--- ### options @@ -83,9 +83,9 @@ ___ #### Defined in -[src/context.ts:16](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L16) +[src/context.ts:16](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L16) -___ +--- ### renderFunction @@ -97,19 +97,15 @@ ___ ##### Parameters -| Name | Type | -| :------ | :------ | -| `c` | [`Context`](../README.md#context)<`TEntity`, `TExtraProperties`\> | +| Name | Type | +| :--- | :---------------------------------------------------------------- | +| `c` | [`Context`](../README.md#context)<`TEntity`, `TExtraProperties`\> | ##### Returns [`DocumentObject`](../README.md#documentobject) -#### Defined in - -[src/context.ts:18](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L18) - -___ +--- ### transformer @@ -117,7 +113,7 @@ ___ #### Defined in -[src/context.ts:14](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L14) +[src/context.ts:14](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L14) ## Methods @@ -131,9 +127,9 @@ ___ #### Defined in -[src/context.ts:60](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L60) +[src/context.ts:60](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L60) -___ +--- ### toContext @@ -145,9 +141,9 @@ ___ #### Defined in -[src/context.ts:43](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L43) +[src/context.ts:43](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L43) -___ +--- ### withIncluded @@ -155,8 +151,8 @@ ___ #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :--------- | :-------- | | `included` | `boolean` | #### Returns @@ -165,9 +161,9 @@ ___ #### Defined in -[src/context.ts:31](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L31) +[src/context.ts:31](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L31) -___ +--- ### withInput @@ -175,8 +171,8 @@ ___ #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :------ | :-------- | | `input` | `TEntity` | #### Returns @@ -185,9 +181,9 @@ ___ #### Defined in -[src/context.ts:20](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L20) +[src/context.ts:20](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L20) -___ +--- ### withOptions @@ -195,8 +191,8 @@ ___ #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :-------- | :----------------------------------------------------- | | `options` | [`Options`](../README.md#options)<`TExtraProperties`\> | #### Returns @@ -205,9 +201,9 @@ ___ #### Defined in -[src/context.ts:37](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L37) +[src/context.ts:37](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L37) -___ +--- ### withTransformer @@ -215,8 +211,8 @@ ___ #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :------------ | :-------------------------------------------------------------- | | `transformer` | [`Transformer`](Transformer.md)<`TEntity`, `TExtraProperties`\> | #### Returns @@ -225,4 +221,4 @@ ___ #### Defined in -[src/context.ts:25](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/context.ts#L25) +[src/context.ts:25](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/context.ts#L25) diff --git a/docs/classes/DefaultTransformer.md b/docs/classes/DefaultTransformer.md index e57ffbd..de0e65f 100644 --- a/docs/classes/DefaultTransformer.md +++ b/docs/classes/DefaultTransformer.md @@ -4,10 +4,10 @@ ## Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `unknown` | -| `TExtraOptions` | `void` | +| Name | Type | +| :-------------- | :-------- | +| `TEntity` | `unknown` | +| `TExtraOptions` | `void` | ## Hierarchy @@ -38,17 +38,17 @@ #### Type parameters -| Name | Type | -| :------ | :------ | -| `TEntity` | `unknown` | -| `TExtraOptions` | `void` | +| Name | Type | +| :-------------- | :-------- | +| `TEntity` | `unknown` | +| `TExtraOptions` | `void` | #### Parameters -| Name | Type | Default value | -| :------ | :------ | :------ | -| `type` | `string` | `undefined` | -| `relationships` | `string`[] \| `Record`<`string`, `string`\> | `[]` | +| Name | Type | Default value | +| :-------------- | :------------------------------------------ | :------------ | +| `type` | `string` | `undefined` | +| `relationships` | `string`[] \| `Record`<`string`, `string`\> | `[]` | #### Overrides @@ -56,7 +56,7 @@ #### Defined in -[src/default-transformer.ts:8](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/default-transformer.ts#L8) +[src/default-transformer.ts:8](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/default-transformer.ts#L8) ## Properties @@ -70,9 +70,9 @@ #### Defined in -[src/default-transformer.ts:6](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/default-transformer.ts#L6) +[src/default-transformer.ts:6](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/default-transformer.ts#L6) -___ +--- ### type @@ -82,10 +82,6 @@ ___ [Transformer](Transformer.md).[type](Transformer.md#type) -#### Defined in - -[src/default-transformer.ts:8](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/default-transformer.ts#L8) - ## Methods ### transform @@ -94,8 +90,8 @@ ___ #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :------- | :---- | | `entity` | `any` | #### Returns @@ -108,4 +104,4 @@ ___ #### Defined in -[src/default-transformer.ts:28](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/default-transformer.ts#L28) +[src/default-transformer.ts:28](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/default-transformer.ts#L28) diff --git a/docs/classes/JsonApiFractalError.md b/docs/classes/JsonApiFractalError.md index 8e624c8..c4ffd82 100644 --- a/docs/classes/JsonApiFractalError.md +++ b/docs/classes/JsonApiFractalError.md @@ -34,8 +34,8 @@ #### Parameters -| Name | Type | -| :------ | :------ | +| Name | Type | +| :-------- | :------- | | `message` | `string` | #### Overrides @@ -44,7 +44,7 @@ Error.constructor #### Defined in -[src/errors.ts:2](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/errors.ts#L2) +[src/errors.ts:2](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/errors.ts#L2) ## Properties @@ -60,7 +60,7 @@ Error.message node_modules/typescript/lib/lib.es5.d.ts:1054 -___ +--- ### name @@ -74,7 +74,7 @@ Error.name node_modules/typescript/lib/lib.es5.d.ts:1053 -___ +--- ### stack @@ -88,7 +88,7 @@ Error.stack node_modules/typescript/lib/lib.es5.d.ts:1055 -___ +--- ### prepareStackTrace @@ -100,15 +100,13 @@ ___ Optional override for formatting stack traces -**`See`** - -https://v8.dev/docs/stack-trace-api#customizing-stack-traces +**`see`** https://v8.dev/docs/stack-trace-api#customizing-stack-traces ##### Parameters -| Name | Type | -| :------ | :------ | -| `err` | `Error` | +| Name | Type | +| :------------ | :----------- | +| `err` | `Error` | | `stackTraces` | `CallSite`[] | ##### Returns @@ -121,9 +119,9 @@ Error.prepareStackTrace #### Defined in -node_modules/@types/node/globals.d.ts:11 +node_modules/@types/node/globals.d.ts:27 -___ +--- ### stackTraceLimit @@ -135,7 +133,7 @@ Error.stackTraceLimit #### Defined in -node_modules/@types/node/globals.d.ts:13 +node_modules/@types/node/globals.d.ts:29 ## Methods @@ -147,9 +145,9 @@ Create .stack property on a target object #### Parameters -| Name | Type | -| :------ | :------ | -| `targetObject` | `object` | +| Name | Type | +| :---------------- | :--------- | +| `targetObject` | `object` | | `constructorOpt?` | `Function` | #### Returns @@ -162,4 +160,4 @@ Error.captureStackTrace #### Defined in -node_modules/@types/node/globals.d.ts:4 +node_modules/@types/node/globals.d.ts:20 diff --git a/docs/classes/Transformer.md b/docs/classes/Transformer.md index 6991ca0..13171fe 100644 --- a/docs/classes/Transformer.md +++ b/docs/classes/Transformer.md @@ -4,9 +4,9 @@ ## Type parameters -| Name | -| :------ | -| `TEntity` | +| Name | +| :-------------- | +| `TEntity` | | `TExtraOptions` | ## Hierarchy @@ -38,14 +38,14 @@ #### Type parameters -| Name | -| :------ | -| `TEntity` | +| Name | +| :-------------- | +| `TEntity` | | `TExtraOptions` | #### Defined in -[src/transformer.ts:8](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L8) +[src/transformer.ts:8](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L8) ## Properties @@ -55,9 +55,9 @@ #### Defined in -[src/transformer.ts:5](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L5) +[src/transformer.ts:5](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L5) -___ +--- ### type @@ -65,7 +65,7 @@ ___ #### Defined in -[src/transformer.ts:4](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L4) +[src/transformer.ts:4](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L4) ## Methods @@ -75,9 +75,9 @@ ___ #### Parameters -| Name | Type | -| :------ | :------ | -| `entity` | `TEntity` | +| Name | Type | +| :-------- | :-------------------------------------------------- | +| `entity` | `TEntity` | | `options` | [`Options`](../README.md#options)<`TExtraOptions`\> | #### Returns @@ -86,4 +86,4 @@ ___ #### Defined in -[src/transformer.ts:6](https://github.com/andersondanilo/jsonapi-fractal/blob/9e4f6c2/src/transformer.ts#L6) +[src/transformer.ts:6](https://github.com/andersondanilo/jsonapi-fractal/blob/43f9c51/src/transformer.ts#L6) diff --git a/src/deserializer.ts b/src/deserializer.ts index dec27a1..6f05e99 100644 --- a/src/deserializer.ts +++ b/src/deserializer.ts @@ -1,4 +1,4 @@ -import { AttributesObject, DocumentObject, ExistingResourceObject, Options, ResourceObject } from './types' +import { AttributesObject, DocumentObject, ExistingResourceObject, DeserializeOptions, ResourceObject } from './types' import { caseTypes, changeCase } from './utils' type IncludedCache = Record> @@ -11,7 +11,7 @@ type IncludedCache = Record> */ export function deserialize( response: DocumentObject, - options: Options = {}, + options: DeserializeOptions = {}, ): TEntity | TEntity[] | undefined { if (!response.data) { return undefined @@ -26,10 +26,12 @@ export function deserialize( : parseJsonApiSimpleResourceData(response.data, included, options, false, {}) } +export const typeField = Symbol('type') + function parseJsonApiSimpleResourceData( data: ResourceObject, included: ExistingResourceObject[], - options: Options, + options: DeserializeOptions, useCache: boolean, includedCache: IncludedCache, ): TEntity { @@ -50,6 +52,7 @@ function parseJsonApiSimpleResourceData( } const resource: Record = { + ...(options.injectType ? { [typeField]: data.type } : {}), ...(id ? { id } : {}), ...attributes, } @@ -113,12 +116,12 @@ function findJsonApiIncluded( includedCache: IncludedCache, type: string, id: string, - options: Options, + options: DeserializeOptions, ): TEntity { const foundResource = included.find((item) => item.type === type && item.id === id) if (!foundResource) { - return { id } as unknown as TEntity + return { id, ...(options.injectType ? { [typeField]: type } : {}), } as unknown as TEntity } return parseJsonApiSimpleResourceData(foundResource, included, options, true, includedCache) diff --git a/src/index.ts b/src/index.ts index dc3f033..d58190b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ export { export * from './types' export { DefaultTransformer } from './default-transformer' export { Context, ContextBuilder } from './context' -export { deserialize } from './deserializer' +export { deserialize, typeField } from './deserializer' export { serialize, transform } from './serializer' export { whitelist } from './utils' export { JsonApiFractalError } from './errors' diff --git a/src/types.ts b/src/types.ts index 66a8ec5..f1d58b2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -60,6 +60,12 @@ export type Options = { extra?: TExtraOptions } + +export type DeserializeOptions = Options & { + /** inject `type` as a symbol */ + injectType?: boolean +} + export type SerializeOptions = Options & { relationships?: string[] | Record included?: boolean diff --git a/tests/deserialize.spec.ts b/tests/deserialize.spec.ts index 707010d..81c6791 100644 --- a/tests/deserialize.spec.ts +++ b/tests/deserialize.spec.ts @@ -1,4 +1,4 @@ -import { deserialize, DocumentObject } from '../src' +import { deserialize, typeField, DocumentObject } from '../src' import { CaseType } from '../src/types' describe('deserialize', () => { @@ -51,6 +51,23 @@ describe('deserialize', () => { images: [{ id: '1' }, { id: '2' }], }, ]) + expect(deserialize(serialized, { changeCase: CaseType.camelCase, injectType: true })).toStrictEqual([ + { + id: '1', + [typeField]: 'users', + firstName: 'Joe', + lastName: 'Doe', + address: { + id: '1', + [typeField]: 'addr', + street: 'Street 1', + }, + images: [ + { id: '1', [typeField]: 'img' }, + { id: '2', [typeField]: 'img' }, + ], + }, + ]) }) it('deserialize includes links', () => { @@ -118,6 +135,31 @@ describe('deserialize', () => { images: [{ id: '1' }, { id: '2' }], }, ]) + expect(deserialize(serialized, { changeCase: CaseType.camelCase, injectType: true })).toStrictEqual([ + { + id: '1', + [typeField]: 'users', + firstName: 'Joe', + lastName: 'Doe', + links: { + self: 'https://example.org/users/1', + action: 'https://example.org/action', + }, + address: { + id: '1', + [typeField]: 'addr', + street: 'Street 1', + links: { + self: 'https://example.org/address/1/relationships/address', + related: 'https://example.org/address/1', + }, + }, + images: [ + { id: '1', [typeField]: 'img' }, + { id: '2', [typeField]: 'img' }, + ], + }, + ]) }) it('should change relationship name casing', () => { @@ -162,5 +204,18 @@ describe('deserialize', () => { }, }, ]) + expect(deserialize(serialized, { changeCase: CaseType.camelCase, injectType: true })).toStrictEqual([ + { + id: '1', + [typeField]: 'users', + firstName: 'Joe', + lastName: 'Doe', + homeAddress: { + id: '1', + [typeField]: 'addr', + street: 'Street 1', + }, + }, + ]) }) })