rest-client-sdk
Advanced tools
Comparing version 5.0.0-rc.3 to 5.0.0-rc.4
# Changelog | ||
## Unreleased | ||
## 5.0.0-rc.1 | ||
@@ -5,0 +5,0 @@ ### Changed |
export default interface AsyncStorageInterface { | ||
setItem(key: string, value: string): Promise<void>; | ||
setItem(key: string, value: string): Promise<unknown>; | ||
getItem(key: string): Promise<string>; | ||
removeItem(key: string): Promise<void>; | ||
} |
import URI from 'urijs'; | ||
import { Token } from '../TokenGenerator/types'; | ||
import type RestClientSdk from '../RestClientSdk'; | ||
import type { SdkMetadata } from '../RestClientSdk'; | ||
import type ClassMetadata from '../Mapping/ClassMetadata'; | ||
import type SerializerInterface from '../serializer/SerializerInterface'; | ||
declare class AbstractClient<E extends object, L extends Iterable<E>, T extends Token> { | ||
declare class AbstractClient<M extends SdkMetadata, K extends keyof M, T extends Token> { | ||
#private; | ||
sdk: RestClientSdk<T>; | ||
sdk: RestClientSdk<M, T>; | ||
serializer: SerializerInterface; | ||
metadata: ClassMetadata; | ||
constructor(sdk: RestClientSdk<T>, metadata: ClassMetadata); | ||
constructor(sdk: RestClientSdk<M, T>, metadata: ClassMetadata); | ||
getDefaultParameters(): object; | ||
getPathBase(pathParameters: object): string; | ||
getEntityURI(entity: E): string; | ||
find(id: string | number, queryParam?: {}, pathParameters?: {}, requestParams?: {}): Promise<E>; | ||
findBy(queryParam: object, pathParameters?: {}, requestParams?: {}): Promise<L>; | ||
findAll(queryParam?: {}, pathParameters?: {}, requestParams?: {}): Promise<L>; | ||
create(entity: E, queryParam?: {}, pathParameters?: {}, requestParams?: {}): Promise<E>; | ||
update(entity: E, queryParam?: {}, requestParams?: {}): Promise<E>; | ||
delete(entity: E, requestParams?: {}): Promise<Response>; | ||
deserializeResponse<LOR extends 'list' | 'item'>(requestPromise: Promise<Response>, listOrItem: LOR): Promise<E | L>; | ||
getEntityURI(entity: M[K][0]): string; | ||
find(id: string | number, queryParam?: {}, pathParameters?: {}, requestParams?: {}): Promise<M[K][0]>; | ||
findBy(queryParam: object, pathParameters?: {}, requestParams?: {}): Promise<M[K][1]>; | ||
findAll(queryParam?: {}, pathParameters?: {}, requestParams?: {}): Promise<M[K][1]>; | ||
create(entity: M[K][0], queryParam?: {}, pathParameters?: {}, requestParams?: {}): Promise<M[K][0]>; | ||
update(entity: M[K][0], queryParam?: {}, requestParams?: {}): Promise<M[K][0]>; | ||
delete(entity: M[K][0], requestParams?: {}): Promise<Response>; | ||
deserializeResponse<LOR extends 'list' | 'item'>(requestPromise: Promise<Response>, listOrItem: LOR): Promise<M[K][0] | M[K][1]>; | ||
makeUri(input: string | URI): URI; | ||
@@ -23,0 +24,0 @@ authorizedFetch(input: string | URI, requestParams?: {}): Promise<Response>; |
@@ -16,4 +16,5 @@ import RestClientSdk from './RestClientSdk'; | ||
import type { Token } from './TokenGenerator/types'; | ||
import type { SdkMetadata } from './RestClientSdk'; | ||
export default RestClientSdk; | ||
export { AbstractClient, AbstractTokenGenerator, ClientCredentialsGenerator, PasswordGenerator, ProvidedTokenGenerator, Serializer, TokenStorage, UnauthorizedError, BadRequestError, ConflictError, ForbiddenError, OauthError, InvalidGrantError, InvalidScopeError, HttpError, InternalServerError, ResourceNotFoundError, Mapping, ClassMetadata, Attribute, Relation, }; | ||
export type { SerializerInterface, Token }; | ||
export type { SerializerInterface, Token, SdkMetadata }; |
@@ -15,3 +15,4 @@ import UnitOfWork from './UnitOfWork'; | ||
}; | ||
declare class RestClientSdk<T extends Token> { | ||
export declare type SdkMetadata = Record<string, [any, Iterable<any>]>; | ||
declare class RestClientSdk<M extends SdkMetadata, T extends Token> { | ||
#private; | ||
@@ -24,3 +25,3 @@ config: Config; | ||
constructor(tokenStorage: TokenStorage<T>, config: Config, mapping: Mapping, serializer?: SerializerInterface); | ||
getRepository<E extends object, L extends Iterable<E>, T extends Token>(key: string): AbstractClient<E, L, T>; | ||
getRepository<K extends keyof M & string>(key: K): AbstractClient<M, K, T>; | ||
private _mergeWithBaseConfig; | ||
@@ -27,0 +28,0 @@ private checkConfigValidity; |
@@ -24,4 +24,4 @@ import TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface'; | ||
getCurrentTokenExpiresIn(): Promise<number | null>; | ||
_storeAccessToken(responseData: T): Promise<void>; | ||
_storeAccessToken(responseData: T): Promise<unknown>; | ||
} | ||
export default TokenStorage; |
{ | ||
"name": "rest-client-sdk", | ||
"version": "5.0.0-rc.3", | ||
"version": "5.0.0-rc.4", | ||
"description": "Rest Client SDK for API", | ||
@@ -37,3 +37,3 @@ "main": "dist/index.js", | ||
"@types/deep-diff": "^1.0.0", | ||
"@types/urijs": "^1.19.8", | ||
"@types/urijs": "^1.19.9", | ||
"@typescript-eslint/eslint-plugin": "^2.26.0", | ||
@@ -40,0 +40,0 @@ "@typescript-eslint/parser": "^2.26.0", |
@@ -57,2 +57,24 @@ # Mapado Rest Client JS SDK [![Build Status](https://travis-ci.org/mapado/rest-client-js-sdk.svg?branch=master)](https://travis-ci.org/mapado/rest-client-js-sdk) | ||
Typescript users: | ||
```ts | ||
type Product = { | ||
@id: string; | ||
name: string; | ||
categoryList: Category[]; | ||
}; | ||
type Category = { | ||
@id: string; | ||
name: string; | ||
product: Product; | ||
}; | ||
type TSMetadata = { | ||
// first value is the entity object, second one is the listing type, it can be any Iterable<Entity> | ||
products: [Product, Array<Product>]; | ||
categories: [Category, Array<Category>]; | ||
}; | ||
``` | ||
### Create the SDK | ||
@@ -99,2 +121,10 @@ | ||
Typescript users: | ||
```ts | ||
import RestClientSdk, { Token } from 'rest-client-sdk'; | ||
const sdk = new RestClientSdk<TSMetadata, Token>(tokenStorage, config, mapping); | ||
``` | ||
### Make calls | ||
@@ -114,2 +144,5 @@ | ||
All these methods returns promises. | ||
`find` returns a `Promise<Entity>`, `findBy` and `findAll` returns `Promise<Iterable<Entity>>` | ||
#### Update / delete | ||
@@ -125,2 +158,6 @@ | ||
All these methods returns promises. | ||
`create` and `update` returns a `Promise<Entity>` with the new entity. | ||
`delete` returns `Promise<void>`. | ||
### Overriding repository | ||
@@ -146,2 +183,22 @@ | ||
Typescript users: | ||
```ts | ||
import { SdkMetadata } from 'rest-client-sdk'; | ||
class SomeEntityClient< | ||
M extends SdkMetadata, | ||
K extends keyof M, | ||
T extends Token | ||
> extends AbstractClient<M, K, T> { | ||
getPathBase(pathParameters: object) { | ||
return '/v2/some_entities'; // you need to return the full query string for the collection GET query | ||
} | ||
getEntityURI(entity: M[K][0]) { | ||
return `${this.getPathBase()}/${entity.id}`; // this will be the URI used by update / delete script | ||
} | ||
} | ||
``` | ||
### Custom serializer | ||
@@ -189,1 +246,4 @@ | ||
``` | ||
Typescript users: | ||
@TODO |
export default interface AsyncStorageInterface { | ||
setItem(key: string, value: string): Promise<void>; | ||
setItem(key: string, value: string): Promise<unknown>; | ||
@@ -4,0 +4,0 @@ getItem(key: string): Promise<string>; |
@@ -6,3 +6,6 @@ import URI from 'urijs'; | ||
import { removeAuthorization, removeUndefinedHeaders } from './headerUtils'; | ||
// eslint-disable-next-line import/no-duplicates | ||
import type RestClientSdk from '../RestClientSdk'; | ||
// eslint-disable-next-line import/no-duplicates | ||
import type { SdkMetadata } from '../RestClientSdk'; | ||
import type ClassMetadata from '../Mapping/ClassMetadata'; | ||
@@ -13,4 +16,8 @@ import type SerializerInterface from '../serializer/SerializerInterface'; | ||
class AbstractClient<E extends object, L extends Iterable<E>, T extends Token> { | ||
sdk: RestClientSdk<T>; | ||
class AbstractClient< | ||
M extends SdkMetadata, | ||
K extends keyof M, | ||
T extends Token | ||
> { | ||
sdk: RestClientSdk<M, T>; | ||
@@ -23,3 +30,3 @@ #tokenStorage: TokenStorage<T>; | ||
constructor(sdk: RestClientSdk<T>, metadata: ClassMetadata) { | ||
constructor(sdk: RestClientSdk<M, T>, metadata: ClassMetadata) { | ||
this.sdk = sdk; | ||
@@ -40,3 +47,3 @@ this.#tokenStorage = sdk.tokenStorage; | ||
getEntityURI(entity: E): string { | ||
getEntityURI(entity: M[K][0]): string { | ||
let idValue = this._getEntityIdentifier(entity); | ||
@@ -68,3 +75,3 @@ | ||
requestParams = {} | ||
): Promise<E> { | ||
): Promise<M[K][0]> { | ||
const url = this._generateUrlFromParams(queryParam, pathParameters, id); | ||
@@ -75,3 +82,3 @@ | ||
'item' | ||
) as Promise<E>; | ||
) as Promise<M[K][0]>; | ||
} | ||
@@ -83,3 +90,3 @@ | ||
requestParams = {} | ||
): Promise<L> { | ||
): Promise<M[K][1]> { | ||
const url = this._generateUrlFromParams(queryParam, pathParameters); | ||
@@ -90,3 +97,3 @@ | ||
'list' | ||
) as Promise<L>; | ||
) as Promise<M[K][1]>; | ||
} | ||
@@ -98,3 +105,3 @@ | ||
requestParams = {} | ||
): Promise<L> { | ||
): Promise<M[K][1]> { | ||
return this.findBy(queryParam, pathParameters, requestParams); | ||
@@ -104,7 +111,7 @@ } | ||
create( | ||
entity: E, | ||
entity: M[K][0], | ||
queryParam = {}, | ||
pathParameters = {}, | ||
requestParams = {} | ||
): Promise<E> { | ||
): Promise<M[K][0]> { | ||
const url = new URI(this.getPathBase(pathParameters)); | ||
@@ -132,6 +139,10 @@ url.addSearch(queryParam); | ||
'item' | ||
) as Promise<E>; | ||
) as Promise<M[K][0]>; | ||
} | ||
update(entity: E, queryParam = {}, requestParams = {}): Promise<E> { | ||
update( | ||
entity: M[K][0], | ||
queryParam = {}, | ||
requestParams = {} | ||
): Promise<M[K][0]> { | ||
const url = new URI(this.getEntityURI(entity)); | ||
@@ -166,6 +177,6 @@ url.addSearch(queryParam); | ||
'item' | ||
) as Promise<E>; | ||
) as Promise<M[K][0]>; | ||
} | ||
delete(entity: E, requestParams = {}): Promise<Response> { | ||
delete(entity: M[K][0], requestParams = {}): Promise<Response> { | ||
const url = this.getEntityURI(entity); | ||
@@ -189,3 +200,3 @@ const identifier = this._getEntityIdentifier(entity); | ||
listOrItem: LOR | ||
): Promise<E | L> { | ||
): Promise<M[K][0] | M[K][1]> { | ||
return requestPromise | ||
@@ -196,3 +207,3 @@ .then((response) => response.text().then((text) => ({ response, text }))) | ||
// for list, we need to deserialize the result to get an object | ||
const itemList = this.serializer.deserializeList<E, L>( | ||
const itemList = this.serializer.deserializeList<M[K][0], M[K][1]>( | ||
text, | ||
@@ -217,3 +228,3 @@ this.metadata, | ||
return itemList as L; | ||
return itemList as M[K][1]; | ||
} | ||
@@ -232,3 +243,3 @@ | ||
// and finally return the denormalized item | ||
const item = this.serializer.denormalizeItem<E>( | ||
const item = this.serializer.denormalizeItem<M[K][0]>( | ||
decodedItem, | ||
@@ -246,3 +257,3 @@ this.metadata, | ||
return item as E; | ||
return item as M[K][0]; | ||
}); | ||
@@ -249,0 +260,0 @@ } |
@@ -27,2 +27,3 @@ import RestClientSdk from './RestClientSdk'; | ||
import type { Token } from './TokenGenerator/types'; | ||
import type { SdkMetadata } from './RestClientSdk'; | ||
@@ -53,2 +54,2 @@ export default RestClientSdk; | ||
}; | ||
export type { SerializerInterface, Token }; | ||
export type { SerializerInterface, Token, SdkMetadata }; |
@@ -18,3 +18,5 @@ import JsSerializer from './serializer/JsSerializer'; | ||
class RestClientSdk<T extends Token> { | ||
export type SdkMetadata = Record<string, [any, Iterable<any>]>; | ||
class RestClientSdk<M extends SdkMetadata, T extends Token> { | ||
config: Config; | ||
@@ -30,3 +32,3 @@ | ||
#repositoryList: { [key: string]: AbstractClient<any, any, any> }; | ||
#repositoryList: Partial<Record<keyof M, AbstractClient<M, keyof M, any>>>; | ||
@@ -55,5 +57,3 @@ constructor( | ||
getRepository<E extends object, L extends Iterable<E>, T extends Token>( | ||
key: string | ||
): AbstractClient<E, L, T> { | ||
getRepository<K extends keyof M & string>(key: K): AbstractClient<M, K, T> { | ||
if (!this.#repositoryList[key]) { | ||
@@ -67,9 +67,7 @@ const metadata = this.mapping.getClassMetadataByKey(key); | ||
// eslint-disable-next-line new-cap | ||
this.#repositoryList[key] = (new metadata.repositoryClass( | ||
this, | ||
metadata | ||
) as unknown) as AbstractClient<E, L, T>; | ||
this.#repositoryList[key] = new metadata.repositoryClass(this, metadata); | ||
} | ||
return this.#repositoryList[key]; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.#repositoryList[key]!; | ||
} | ||
@@ -76,0 +74,0 @@ |
@@ -154,3 +154,3 @@ /* eslint-disable @typescript-eslint/camelcase */ | ||
_storeAccessToken(responseData: T): Promise<void> { | ||
_storeAccessToken(responseData: T): Promise<unknown> { | ||
return this.#asyncStorage.setItem( | ||
@@ -157,0 +157,0 @@ this.accessTokenKey, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
495012
6569
244