From 79857694252818c995d7ca5ee83494b7da623c18 Mon Sep 17 00:00:00 2001 From: Michael Hirn Date: Thu, 4 Jun 2020 23:17:41 +0100 Subject: [PATCH] feat(response): slim response API removes IEntity and leaves it up to IResponsePayload to define such behavior --- docs/markdown/core.ientity.md | 16 - .../core.ientityconstructor.fromresources.md | 14 - docs/markdown/core.ientityconstructor.md | 39 -- docs/markdown/core.iresponse.md | 3 +- docs/markdown/core.iresponse.toentity.md | 27 -- docs/markdown/core.md | 4 +- docs/markdown/core.resourcepayload.md | 22 + docs/markdown/core.resourcepayload.sha1.md | 24 + ...w_.md => core.response_2._constructor_.md} | 14 +- docs/markdown/core.response_2.main.md | 14 + docs/markdown/core.response_2.md | 38 ++ docs/markdown/core.response_2.meta.md | 14 + docs/markdown/core.response_2.page.md | 14 + docs/markdown/core.response_2.toresources.md | 20 + docs/markdown/core.response_2.xhrs.md | 14 + etc/core.api.json | 432 +++++++++++------- etc/core.api.md | 34 +- etc/tmp/core.api.md | 34 +- src/index.ts | 16 +- src/{response => }/resource.ts | 0 src/{response => }/response.ts | 38 +- src/response/entity.ts | 27 -- src/response/index.ts | 17 - test/response.spec.ts | 47 ++ 24 files changed, 578 insertions(+), 344 deletions(-) delete mode 100644 docs/markdown/core.ientity.md delete mode 100644 docs/markdown/core.ientityconstructor.fromresources.md delete mode 100644 docs/markdown/core.ientityconstructor.md delete mode 100644 docs/markdown/core.iresponse.toentity.md create mode 100644 docs/markdown/core.resourcepayload.md create mode 100644 docs/markdown/core.resourcepayload.sha1.md rename docs/markdown/{core.ientityconstructor._new_.md => core.response_2._constructor_.md} (52%) create mode 100644 docs/markdown/core.response_2.main.md create mode 100644 docs/markdown/core.response_2.md create mode 100644 docs/markdown/core.response_2.meta.md create mode 100644 docs/markdown/core.response_2.page.md create mode 100644 docs/markdown/core.response_2.toresources.md create mode 100644 docs/markdown/core.response_2.xhrs.md rename src/{response => }/resource.ts (100%) rename src/{response => }/response.ts (71%) delete mode 100644 src/response/entity.ts delete mode 100644 src/response/index.ts create mode 100644 test/response.spec.ts diff --git a/docs/markdown/core.ientity.md b/docs/markdown/core.ientity.md deleted file mode 100644 index b0ad654..0000000 --- a/docs/markdown/core.ientity.md +++ /dev/null @@ -1,16 +0,0 @@ - - -[Home](./index.md) > [@authless/core](./core.md) > [IEntity](./core.ientity.md) - -## IEntity interface - -> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. -> - -A entity usually extracted from a [IResponse](./core.iresponse.md) via [IResponse.toEntity()](./core.iresponse.toentity.md). - -Signature: - -```typescript -export interface IEntity -``` diff --git a/docs/markdown/core.ientityconstructor.fromresources.md b/docs/markdown/core.ientityconstructor.fromresources.md deleted file mode 100644 index b7ff85b..0000000 --- a/docs/markdown/core.ientityconstructor.fromresources.md +++ /dev/null @@ -1,14 +0,0 @@ - - -[Home](./index.md) > [@authless/core](./core.md) > [IEntityConstructor](./core.ientityconstructor.md) > [fromResources](./core.ientityconstructor.fromresources.md) - -## IEntityConstructor.fromResources property - -> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. -> - -Signature: - -```typescript -fromResources: (resources: IResourcePayload[]) => IEntity; -``` diff --git a/docs/markdown/core.ientityconstructor.md b/docs/markdown/core.ientityconstructor.md deleted file mode 100644 index 17ba398..0000000 --- a/docs/markdown/core.ientityconstructor.md +++ /dev/null @@ -1,39 +0,0 @@ - - -[Home](./index.md) > [@authless/core](./core.md) > [IEntityConstructor](./core.ientityconstructor.md) - -## IEntityConstructor interface - -> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. -> - -A EntityConstructor used to build [IEntity](./core.ientity.md) from [Resources](./core.iresourcepayload.md). - -Signature: - -```typescript -export interface IEntityConstructor -``` - -## Example - - -```ts -const response: IResponse = { ... } -const resourceResponse = response.toResources() -const entity = EntityConstructor.fromResources(resourceResponse.toArray()) - -``` - -## Properties - -| Property | Type | Description | -| --- | --- | --- | -| [fromResources](./core.ientityconstructor.fromresources.md) | (resources: [IResourcePayload](./core.iresourcepayload.md)\[\]) => [IEntity](./core.ientity.md) | (BETA) | - -## Methods - -| Method | Description | -| --- | --- | -| [(new)(args)](./core.ientityconstructor._new_.md) | (BETA) | - diff --git a/docs/markdown/core.iresponse.md b/docs/markdown/core.iresponse.md index e0b2361..2a58c06 100644 --- a/docs/markdown/core.iresponse.md +++ b/docs/markdown/core.iresponse.md @@ -17,7 +17,7 @@ export interface IResponse ## Remarks -A [IResponse](./core.iresponse.md) can be transformed into [IResourceResponse](./core.iresourceresponse.md) or [IEntity](./core.ientity.md) responses which extract the more and the most relevant data from an [IResponse](./core.iresponse.md). +A [IResponse](./core.iresponse.md) can be transformed into a [IResourceResponse](./core.iresourceresponse.md) which extracts the most relevant data from an [IResponse](./core.iresponse.md). Service repositories should create their own response class implementing [IResponse](./core.iresponse.md). @@ -35,6 +35,5 @@ Service repositories should create their own response class implementing [IRespo | Method | Description | | --- | --- | -| [toEntity(entity)](./core.iresponse.toentity.md) | (BETA) Creates an [IEntity](./core.ientity.md) from an [IResponse](./core.iresponse.md) instance. | | [toResources()](./core.iresponse.toresources.md) | (BETA) Creates a [IResponseResponse](./core.iresponseresponse.md) from an [IResponse](./core.iresponse.md) instance. | diff --git a/docs/markdown/core.iresponse.toentity.md b/docs/markdown/core.iresponse.toentity.md deleted file mode 100644 index 32b0f35..0000000 --- a/docs/markdown/core.iresponse.toentity.md +++ /dev/null @@ -1,27 +0,0 @@ - - -[Home](./index.md) > [@authless/core](./core.md) > [IResponse](./core.iresponse.md) > [toEntity](./core.iresponse.toentity.md) - -## IResponse.toEntity() method - -> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. -> - -Creates an [IEntity](./core.ientity.md) from an [IResponse](./core.iresponse.md) instance. - -Signature: - -```typescript -toEntity(entity: IEntityConstructor): IEntity; -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| entity | [IEntityConstructor](./core.ientityconstructor.md) | | - -Returns: - -[IEntity](./core.ientity.md) - diff --git a/docs/markdown/core.md b/docs/markdown/core.md index 2bf8c7f..1eb6751 100644 --- a/docs/markdown/core.md +++ b/docs/markdown/core.md @@ -14,14 +14,14 @@ Provides the core abstractions and functionality. | Class | Description | | --- | --- | +| [ResourcePayload](./core.resourcepayload.md) | (BETA) | | [ResourceResponse](./core.resourceresponse.md) | (BETA) Abstract implementation of [IResourceResponse](./core.iresourceresponse.md). Extends [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). | +| [Response\_2](./core.response_2.md) | (BETA) see [IResponse](./core.iresponse.md) | ## Interfaces | Interface | Description | | --- | --- | -| [IEntity](./core.ientity.md) | (BETA) A entity usually extracted from a [IResponse](./core.iresponse.md) via [IResponse.toEntity()](./core.iresponse.toentity.md). | -| [IEntityConstructor](./core.ientityconstructor.md) | (BETA) A EntityConstructor used to build [IEntity](./core.ientity.md) from [Resources](./core.iresourcepayload.md). | | [IResourcePayload](./core.iresourcepayload.md) | (BETA) | | [IResourceResponse](./core.iresourceresponse.md) | (BETA) Holds none, one, or many [Resources](./core.iresourcepayload.md) and is usually created via [IResponse.toResources()](./core.iresponse.toresources.md). | | [IResponse](./core.iresponse.md) | (BETA) The raw response from a service including any (xhrs) requests and responses and meta information. | diff --git a/docs/markdown/core.resourcepayload.md b/docs/markdown/core.resourcepayload.md new file mode 100644 index 0000000..726d93c --- /dev/null +++ b/docs/markdown/core.resourcepayload.md @@ -0,0 +1,22 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [ResourcePayload](./core.resourcepayload.md) + +## ResourcePayload class + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + + +Signature: + +```typescript +export declare abstract class ResourcePayload implements IResourcePayload +``` + +## Methods + +| Method | Modifiers | Description | +| --- | --- | --- | +| [sha1()](./core.resourcepayload.sha1.md) | | (BETA) See [IResourcePayload.sha1()](./core.iresourcepayload.sha1.md). | + diff --git a/docs/markdown/core.resourcepayload.sha1.md b/docs/markdown/core.resourcepayload.sha1.md new file mode 100644 index 0000000..71ce90e --- /dev/null +++ b/docs/markdown/core.resourcepayload.sha1.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [ResourcePayload](./core.resourcepayload.md) > [sha1](./core.resourcepayload.sha1.md) + +## ResourcePayload.sha1() method + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +See [IResourcePayload.sha1()](./core.iresourcepayload.sha1.md). + +Signature: + +```typescript +sha1(): string; +``` +Returns: + +string + +## Remarks + +Does not omit any properties of the resource + diff --git a/docs/markdown/core.ientityconstructor._new_.md b/docs/markdown/core.response_2._constructor_.md similarity index 52% rename from docs/markdown/core.ientityconstructor._new_.md rename to docs/markdown/core.response_2._constructor_.md index c83a98e..e9a71c0 100644 --- a/docs/markdown/core.ientityconstructor._new_.md +++ b/docs/markdown/core.response_2._constructor_.md @@ -1,25 +1,23 @@ -[Home](./index.md) > [@authless/core](./core.md) > [IEntityConstructor](./core.ientityconstructor.md) > [(new)](./core.ientityconstructor._new_.md) +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) > [(constructor)](./core.response_2._constructor_.md) -## IEntityConstructor.(new) +## Response\_2.(constructor) > This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. > +Constructs a new instance of the `Response` class + Signature: ```typescript -new (...args: any[]): IEntity; +constructor(serializedResponse: any); ``` ## Parameters | Parameter | Type | Description | | --- | --- | --- | -| args | any\[\] | | - -Returns: - -[IEntity](./core.ientity.md) +| serializedResponse | any | | diff --git a/docs/markdown/core.response_2.main.md b/docs/markdown/core.response_2.main.md new file mode 100644 index 0000000..5d5598f --- /dev/null +++ b/docs/markdown/core.response_2.main.md @@ -0,0 +1,14 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) > [main](./core.response_2.main.md) + +## Response\_2.main property + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Signature: + +```typescript +main: IResponseResponse; +``` diff --git a/docs/markdown/core.response_2.md b/docs/markdown/core.response_2.md new file mode 100644 index 0000000..e5eb6c8 --- /dev/null +++ b/docs/markdown/core.response_2.md @@ -0,0 +1,38 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) + +## Response\_2 class + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +see [IResponse](./core.iresponse.md) + +Signature: + +```typescript +export declare abstract class Response implements IResponse +``` + +## Constructors + +| Constructor | Modifiers | Description | +| --- | --- | --- | +| [(constructor)(serializedResponse)](./core.response_2._constructor_.md) | | (BETA) Constructs a new instance of the Response class | + +## Properties + +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [main](./core.response_2.main.md) | | [IResponseResponse](./core.iresponseresponse.md) | (BETA) | +| [meta](./core.response_2.meta.md) | | [IResponseMeta](./core.iresponsemeta.md) | (BETA) | +| [page](./core.response_2.page.md) | | [IResponsePage](./core.iresponsepage.md) | (BETA) | +| [xhrs](./core.response_2.xhrs.md) | | [IResponseResponse](./core.iresponseresponse.md)\[\] | (BETA) | + +## Methods + +| Method | Modifiers | Description | +| --- | --- | --- | +| [toResources()](./core.response_2.toresources.md) | | (BETA) see [IResponse.toResources()](./core.iresponse.toresources.md). Needs to be implemented by services. | + diff --git a/docs/markdown/core.response_2.meta.md b/docs/markdown/core.response_2.meta.md new file mode 100644 index 0000000..4c22ce9 --- /dev/null +++ b/docs/markdown/core.response_2.meta.md @@ -0,0 +1,14 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) > [meta](./core.response_2.meta.md) + +## Response\_2.meta property + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Signature: + +```typescript +meta: IResponseMeta; +``` diff --git a/docs/markdown/core.response_2.page.md b/docs/markdown/core.response_2.page.md new file mode 100644 index 0000000..1d68ccb --- /dev/null +++ b/docs/markdown/core.response_2.page.md @@ -0,0 +1,14 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) > [page](./core.response_2.page.md) + +## Response\_2.page property + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Signature: + +```typescript +page: IResponsePage; +``` diff --git a/docs/markdown/core.response_2.toresources.md b/docs/markdown/core.response_2.toresources.md new file mode 100644 index 0000000..8ed4e47 --- /dev/null +++ b/docs/markdown/core.response_2.toresources.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) > [toResources](./core.response_2.toresources.md) + +## Response\_2.toResources() method + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +see [IResponse.toResources()](./core.iresponse.toresources.md). Needs to be implemented by services. + +Signature: + +```typescript +toResources(): ResourceResponse; +``` +Returns: + +[ResourceResponse](./core.resourceresponse.md)<[ResourcePayload](./core.resourcepayload.md)> + diff --git a/docs/markdown/core.response_2.xhrs.md b/docs/markdown/core.response_2.xhrs.md new file mode 100644 index 0000000..134e3df --- /dev/null +++ b/docs/markdown/core.response_2.xhrs.md @@ -0,0 +1,14 @@ + + +[Home](./index.md) > [@authless/core](./core.md) > [Response\_2](./core.response_2.md) > [xhrs](./core.response_2.xhrs.md) + +## Response\_2.xhrs property + +> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. +> + +Signature: + +```typescript +xhrs: IResponseResponse[]; +``` diff --git a/etc/core.api.json b/etc/core.api.json index 941f42f..2586165 100644 --- a/etc/core.api.json +++ b/etc/core.api.json @@ -15,119 +15,6 @@ "canonicalReference": "@authless/core!", "name": "", "members": [ - { - "kind": "Interface", - "canonicalReference": "@authless/core!IEntity:interface", - "docComment": "/**\n * A entity usually extracted from a {@link IResponse} via {@link IResponse.toEntity}.\n *\n * @beta\n */\n", - "excerptTokens": [ - { - "kind": "Content", - "text": "export interface IEntity " - } - ], - "releaseTag": "Beta", - "name": "IEntity", - "members": [], - "extendsTokenRanges": [] - }, - { - "kind": "Interface", - "canonicalReference": "@authless/core!IEntityConstructor:interface", - "docComment": "/**\n * A EntityConstructor used to build {@link IEntity} from {@link IResourcePayload | Resources}.\n *\n * @example\n * ```ts\n * const response: IResponse = { ... }\n * const resourceResponse = response.toResources()\n * const entity = EntityConstructor.fromResources(resourceResponse.toArray())\n * ```\n *\n * @beta\n */\n", - "excerptTokens": [ - { - "kind": "Content", - "text": "export interface IEntityConstructor " - } - ], - "releaseTag": "Beta", - "name": "IEntityConstructor", - "members": [ - { - "kind": "ConstructSignature", - "canonicalReference": "@authless/core!IEntityConstructor:new(1)", - "docComment": "", - "excerptTokens": [ - { - "kind": "Content", - "text": "new (...args: " - }, - { - "kind": "Content", - "text": "any[]" - }, - { - "kind": "Content", - "text": "): " - }, - { - "kind": "Reference", - "text": "IEntity", - "canonicalReference": "@authless/core!IEntity:interface" - }, - { - "kind": "Content", - "text": ";" - } - ], - "returnTypeTokenRange": { - "startIndex": 3, - "endIndex": 4 - }, - "releaseTag": "Beta", - "overloadIndex": 1, - "parameters": [ - { - "parameterName": "args", - "parameterTypeTokenRange": { - "startIndex": 1, - "endIndex": 2 - } - } - ] - }, - { - "kind": "PropertySignature", - "canonicalReference": "@authless/core!IEntityConstructor#fromResources:member", - "docComment": "", - "excerptTokens": [ - { - "kind": "Content", - "text": "fromResources: " - }, - { - "kind": "Content", - "text": "(resources: " - }, - { - "kind": "Reference", - "text": "IResourcePayload", - "canonicalReference": "@authless/core!IResourcePayload:interface" - }, - { - "kind": "Content", - "text": "[]) => " - }, - { - "kind": "Reference", - "text": "IEntity", - "canonicalReference": "@authless/core!IEntity:interface" - }, - { - "kind": "Content", - "text": ";" - } - ], - "releaseTag": "Beta", - "name": "fromResources", - "propertyTypeTokenRange": { - "startIndex": 1, - "endIndex": 5 - } - } - ], - "extendsTokenRanges": [] - }, { "kind": "Interface", "canonicalReference": "@authless/core!IResourcePayload:interface", @@ -239,7 +126,7 @@ { "kind": "Interface", "canonicalReference": "@authless/core!IResponse:interface", - "docComment": "/**\n * The raw response from a service including any (xhrs) requests and responses and meta information.\n *\n * @remarks\n *\n * A {@link IResponse} can be transformed into {@link IResourceResponse} or {@link IEntity} responses which extract the more and the most relevant data from an {@link IResponse}.\n *\n * Service repositories should create their own response class implementing {@link IResponse}.\n *\n * @beta\n */\n", + "docComment": "/**\n * The raw response from a service including any (xhrs) requests and responses and meta information.\n *\n * @remarks\n *\n * A {@link IResponse} can be transformed into a {@link IResourceResponse} which extracts the most relevant data from an {@link IResponse}.\n *\n * Service repositories should create their own response class implementing {@link IResponse}.\n *\n * @beta\n */\n", "excerptTokens": [ { "kind": "Content", @@ -352,51 +239,6 @@ "endIndex": 2 } }, - { - "kind": "MethodSignature", - "canonicalReference": "@authless/core!IResponse#toEntity:member(1)", - "docComment": "/**\n * Creates an {@link IEntity} from an {@link IResponse} instance.\n */\n", - "excerptTokens": [ - { - "kind": "Content", - "text": "toEntity(entity: " - }, - { - "kind": "Reference", - "text": "IEntityConstructor", - "canonicalReference": "@authless/core!IEntityConstructor:interface" - }, - { - "kind": "Content", - "text": "): " - }, - { - "kind": "Reference", - "text": "IEntity", - "canonicalReference": "@authless/core!IEntity:interface" - }, - { - "kind": "Content", - "text": ";" - } - ], - "returnTypeTokenRange": { - "startIndex": 3, - "endIndex": 4 - }, - "releaseTag": "Beta", - "overloadIndex": 1, - "parameters": [ - { - "parameterName": "entity", - "parameterTypeTokenRange": { - "startIndex": 1, - "endIndex": 2 - } - } - ], - "name": "toEntity" - }, { "kind": "MethodSignature", "canonicalReference": "@authless/core!IResponse#toResources:member(1)", @@ -1146,6 +988,64 @@ "endIndex": 4 } }, + { + "kind": "Class", + "canonicalReference": "@authless/core!ResourcePayload:class", + "docComment": "/**\n * @beta\n */\n", + "excerptTokens": [ + { + "kind": "Content", + "text": "export declare abstract class ResourcePayload implements " + }, + { + "kind": "Reference", + "text": "IResourcePayload", + "canonicalReference": "@authless/core!IResourcePayload:interface" + }, + { + "kind": "Content", + "text": " " + } + ], + "releaseTag": "Beta", + "name": "ResourcePayload", + "members": [ + { + "kind": "Method", + "canonicalReference": "@authless/core!ResourcePayload#sha1:member(1)", + "docComment": "/**\n * See {@link IResourcePayload.sha1}.\n *\n * @remarks\n *\n * Does not omit any properties of the resource\n */\n", + "excerptTokens": [ + { + "kind": "Content", + "text": "sha1(): " + }, + { + "kind": "Content", + "text": "string" + }, + { + "kind": "Content", + "text": ";" + } + ], + "isStatic": false, + "returnTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + }, + "releaseTag": "Beta", + "overloadIndex": 1, + "parameters": [], + "name": "sha1" + } + ], + "implementsTokenRanges": [ + { + "startIndex": 1, + "endIndex": 3 + } + ] + }, { "kind": "Class", "canonicalReference": "@authless/core!ResourceResponse:class", @@ -1224,6 +1124,220 @@ "endIndex": 5 }, "implementsTokenRanges": [] + }, + { + "kind": "Class", + "canonicalReference": "@authless/core!Response_2:class", + "docComment": "/**\n * see {@link IResponse}\n *\n * @beta\n */\n", + "excerptTokens": [ + { + "kind": "Content", + "text": "export declare abstract class Response implements " + }, + { + "kind": "Reference", + "text": "IResponse", + "canonicalReference": "@authless/core!IResponse:interface" + }, + { + "kind": "Content", + "text": " " + } + ], + "releaseTag": "Beta", + "name": "Response_2", + "members": [ + { + "kind": "Constructor", + "canonicalReference": "@authless/core!Response_2:constructor(1)", + "docComment": "/**\n * Constructs a new instance of the `Response` class\n */\n", + "excerptTokens": [ + { + "kind": "Content", + "text": "constructor(serializedResponse: " + }, + { + "kind": "Content", + "text": "any" + }, + { + "kind": "Content", + "text": ");" + } + ], + "releaseTag": "Beta", + "overloadIndex": 1, + "parameters": [ + { + "parameterName": "serializedResponse", + "parameterTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + } + } + ] + }, + { + "kind": "Property", + "canonicalReference": "@authless/core!Response_2#main:member", + "docComment": "", + "excerptTokens": [ + { + "kind": "Content", + "text": "main: " + }, + { + "kind": "Reference", + "text": "IResponseResponse", + "canonicalReference": "@authless/core!IResponseResponse:interface" + }, + { + "kind": "Content", + "text": ";" + } + ], + "releaseTag": "Beta", + "name": "main", + "propertyTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + }, + "isStatic": false + }, + { + "kind": "Property", + "canonicalReference": "@authless/core!Response_2#meta:member", + "docComment": "", + "excerptTokens": [ + { + "kind": "Content", + "text": "meta: " + }, + { + "kind": "Reference", + "text": "IResponseMeta", + "canonicalReference": "@authless/core!IResponseMeta:interface" + }, + { + "kind": "Content", + "text": ";" + } + ], + "releaseTag": "Beta", + "name": "meta", + "propertyTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + }, + "isStatic": false + }, + { + "kind": "Property", + "canonicalReference": "@authless/core!Response_2#page:member", + "docComment": "", + "excerptTokens": [ + { + "kind": "Content", + "text": "page: " + }, + { + "kind": "Reference", + "text": "IResponsePage", + "canonicalReference": "@authless/core!IResponsePage:interface" + }, + { + "kind": "Content", + "text": ";" + } + ], + "releaseTag": "Beta", + "name": "page", + "propertyTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + }, + "isStatic": false + }, + { + "kind": "Method", + "canonicalReference": "@authless/core!Response_2#toResources:member(1)", + "docComment": "/**\n * see {@link IResponse.toResources}. Needs to be implemented by services.\n */\n", + "excerptTokens": [ + { + "kind": "Content", + "text": "toResources(): " + }, + { + "kind": "Reference", + "text": "ResourceResponse", + "canonicalReference": "@authless/core!ResourceResponse:class" + }, + { + "kind": "Content", + "text": "<" + }, + { + "kind": "Reference", + "text": "ResourcePayload", + "canonicalReference": "@authless/core!ResourcePayload:class" + }, + { + "kind": "Content", + "text": ">" + }, + { + "kind": "Content", + "text": ";" + } + ], + "isStatic": false, + "returnTypeTokenRange": { + "startIndex": 1, + "endIndex": 5 + }, + "releaseTag": "Beta", + "overloadIndex": 1, + "parameters": [], + "name": "toResources" + }, + { + "kind": "Property", + "canonicalReference": "@authless/core!Response_2#xhrs:member", + "docComment": "", + "excerptTokens": [ + { + "kind": "Content", + "text": "xhrs: " + }, + { + "kind": "Reference", + "text": "IResponseResponse", + "canonicalReference": "@authless/core!IResponseResponse:interface" + }, + { + "kind": "Content", + "text": "[]" + }, + { + "kind": "Content", + "text": ";" + } + ], + "releaseTag": "Beta", + "name": "xhrs", + "propertyTypeTokenRange": { + "startIndex": 1, + "endIndex": 3 + }, + "isStatic": false + } + ], + "implementsTokenRanges": [ + { + "startIndex": 1, + "endIndex": 3 + } + ] } ] } diff --git a/etc/core.api.md b/etc/core.api.md index df53477..ff74a91 100644 --- a/etc/core.api.md +++ b/etc/core.api.md @@ -65,18 +65,6 @@ export class Authless { }, asyncFn: any): Promise; } -// @beta -export interface IEntity { -} - -// @beta -export interface IEntityConstructor { - // (undocumented) - new (...args: any[]): IEntity; - // (undocumented) - fromResources: (resources: IResourcePayload[]) => IEntity; -} - // @beta (undocumented) export interface IResourcePayload { sha1(): string; @@ -94,7 +82,6 @@ export interface IResponse { main: IResponseResponse; meta: IResponseMeta; page: IResponsePage; - toEntity(entity: IEntityConstructor): IEntity; toResources(): IResourceResponse; xhrs: IResponseResponse[]; } @@ -166,11 +153,32 @@ export const ResourceConstructor: { toHashResourcePair(resources: T[]): [string, T][]; }; +// @beta (undocumented) +export abstract class ResourcePayload implements IResourcePayload { + sha1(): string; +} + // @beta export abstract class ResourceResponse extends Map { toArray(): T[]; } +// @beta +abstract class Response_2 implements IResponse { + constructor(serializedResponse: any); + // (undocumented) + main: IResponseResponse; + // (undocumented) + meta: IResponseMeta; + // (undocumented) + page: IResponsePage; + toResources(): ResourceResponse; + // (undocumented) + xhrs: IResponseResponse[]; +} + +export { Response_2 as Response } + // @alpha (undocumented) export class Route { constructor(method: any, path: any, service: any); diff --git a/etc/tmp/core.api.md b/etc/tmp/core.api.md index df53477..ff74a91 100644 --- a/etc/tmp/core.api.md +++ b/etc/tmp/core.api.md @@ -65,18 +65,6 @@ export class Authless { }, asyncFn: any): Promise; } -// @beta -export interface IEntity { -} - -// @beta -export interface IEntityConstructor { - // (undocumented) - new (...args: any[]): IEntity; - // (undocumented) - fromResources: (resources: IResourcePayload[]) => IEntity; -} - // @beta (undocumented) export interface IResourcePayload { sha1(): string; @@ -94,7 +82,6 @@ export interface IResponse { main: IResponseResponse; meta: IResponseMeta; page: IResponsePage; - toEntity(entity: IEntityConstructor): IEntity; toResources(): IResourceResponse; xhrs: IResponseResponse[]; } @@ -166,11 +153,32 @@ export const ResourceConstructor: { toHashResourcePair(resources: T[]): [string, T][]; }; +// @beta (undocumented) +export abstract class ResourcePayload implements IResourcePayload { + sha1(): string; +} + // @beta export abstract class ResourceResponse extends Map { toArray(): T[]; } +// @beta +abstract class Response_2 implements IResponse { + constructor(serializedResponse: any); + // (undocumented) + main: IResponseResponse; + // (undocumented) + meta: IResponseMeta; + // (undocumented) + page: IResponsePage; + toResources(): ResourceResponse; + // (undocumented) + xhrs: IResponseResponse[]; +} + +export { Response_2 as Response } + // @alpha (undocumented) export class Route { constructor(method: any, path: any, service: any); diff --git a/src/index.ts b/src/index.ts index a6d86f7..480a62a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,4 +13,18 @@ export { Service } from './service' export { Route, Router } from './router' export { Authless } from './authless' export { ServiceDefault } from './services' -export * from './response' +export { + IResourcePayload, + IResourceResponse, + ResourceConstructor, + ResourceResponse, + ResourcePayload +} from './resource' +export { + Response, + IResponse, + IResponseResponse, + IResponseRequest, + IResponsePage, + IResponseMeta +} from './response' diff --git a/src/response/resource.ts b/src/resource.ts similarity index 100% rename from src/response/resource.ts rename to src/resource.ts diff --git a/src/response/response.ts b/src/response.ts similarity index 71% rename from src/response/response.ts rename to src/response.ts index a314c79..ce12411 100644 --- a/src/response/response.ts +++ b/src/response.ts @@ -1,13 +1,17 @@ -import { IEntity, IEntityConstructor } from './entity' -import { IResourcePayload, IResourceResponse } from './resource' +import { + IResourcePayload, + IResourceResponse, + ResourcePayload, + ResourceResponse +} from './resource' /** * The raw response from a service including any (xhrs) requests and responses and meta information. * * @remarks * - * A {@link IResponse} can be transformed into {@link IResourceResponse} or {@link IEntity} responses - * which extract the more and the most relevant data from an {@link IResponse}. + * A {@link IResponse} can be transformed into a {@link IResourceResponse} + * which extracts the most relevant data from an {@link IResponse}. * * Service repositories should create their own response class implementing {@link IResponse}. * @@ -51,11 +55,33 @@ export interface IResponse { * Creates a {@link IResponseResponse} from an {@link IResponse} instance. */ toResources(): IResourceResponse +} + +/** + * see {@link IResponse} + * + * @beta + */ +export abstract class Response implements IResponse { + meta: IResponseMeta + page: IResponsePage + main: IResponseResponse + xhrs: IResponseResponse[] + + constructor (serializedResponse: any) { + this.meta = serializedResponse.meta + this.page = serializedResponse.page + this.main = serializedResponse.main + this.xhrs = serializedResponse.xhrs + } /** - * Creates an {@link IEntity} from an {@link IResponse} instance. + * see {@link IResponse.toResources}. Needs to be implemented by services. */ - toEntity(entity: IEntityConstructor): IEntity + /* eslint-disable-next-line class-methods-use-this */ + toResources (): ResourceResponse { + throw new Error('not implemented yet') + } } /** diff --git a/src/response/entity.ts b/src/response/entity.ts deleted file mode 100644 index 5f9f922..0000000 --- a/src/response/entity.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { IResourcePayload } from './resource' - -/** - * A entity usually extracted from a {@link IResponse} via {@link IResponse.toEntity}. - * - * @beta - */ -/* eslint-disable-next-line @typescript-eslint/no-empty-interface */ -export interface IEntity { } - -/** - * A EntityConstructor used to build {@link IEntity} from {@link IResourcePayload | Resources}. - * - * @example - * - * ```ts - * const response: IResponse = { ... } - * const resourceResponse = response.toResources() - * const entity = EntityConstructor.fromResources(resourceResponse.toArray()) - * ``` - * - * @beta - */ -export interface IEntityConstructor { - new (...args: any[]): IEntity - fromResources: (resources: IResourcePayload[]) => IEntity -} diff --git a/src/response/index.ts b/src/response/index.ts deleted file mode 100644 index f29a4f0..0000000 --- a/src/response/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -export { - IEntity, - IEntityConstructor -} from './entity' -export { - IResourcePayload, - IResourceResponse, - ResourceResponse, - ResourceConstructor -} from './resource' -export { - IResponse, - IResponseResponse, - IResponseRequest, - IResponsePage, - IResponseMeta -} from './response' diff --git a/test/response.spec.ts b/test/response.spec.ts new file mode 100644 index 0000000..24d2742 --- /dev/null +++ b/test/response.spec.ts @@ -0,0 +1,47 @@ +/* eslint-disable max-classes-per-file */ +import * as fs from 'fs-extra' +import * as path from 'path' +import { + ResourceConstructor, + ResourcePayload, + ResourceResponse, + Response +} from '../src' + +class TestResponse extends Response { + /* eslint-disable-next-line class-methods-use-this */ + toResources (): TestResourceResponse { + const resources: TestResourcePayload[] = [ + new TestResourcePayload(), + new TestResourcePayload(), + ] + return new TestResourceResponse( + ResourceConstructor.toHashResourcePair(resources) + ) + } +} + +class TestResourcePayload extends ResourcePayload { + +} + +class TestResourceResponse extends ResourceResponse { + +} + +const responseSerialized = fs.readJsonSync( + path.join(__dirname, './fixtures/fullResponsePayload.json') +) + +describe('Response', () => { + test('it can be initialized', () => { + const response = new TestResponse(responseSerialized) + expect(response.meta.time).toBe(1583140599365) + }) + + test('it transforms to ResourceResponse', () => { + const response = new TestResponse(responseSerialized) + const resources = response.toResources() + expect(resources).toBeInstanceOf(TestResourceResponse) + }) +})