rest-client-sdk
Advanced tools
Comparing version 5.0.0-rc.7 to 5.0.0-rc.8
# Changelog | ||
## 5.0.0-rc.8 | ||
- export (and implement) `RestClientSdkInterface` and `TokenStorageInterface` | ||
- export `TokenGeneratorInterface` and `AsyncStorageInterface` | ||
- Fix wrong parameter with `grant_type` in `generateToken` | ||
## 5.0.0-rc.7 | ||
@@ -37,2 +43,18 @@ | ||
## 4.1.4 | ||
### Changed | ||
- Passing a string as argument to `ProvidedTokenGenerator` is now deprecated. You should pass the token object instead. It should have had a weird comportment before by the way. | ||
### Fixed | ||
- Fix issue when two token generation are send on the same time : the second one will not throw an error now. | ||
## 4.1.3 | ||
### Fixed | ||
- Fix issue when no token is provided but the object is `{ expires_at: null }` | ||
## 4.1.2 | ||
@@ -39,0 +61,0 @@ |
@@ -17,4 +17,8 @@ import RestClientSdk from './RestClientSdk'; | ||
import type { SdkMetadata } from './RestClientSdk'; | ||
import type RestClientSdkInterface from './RestClientSdkInterface'; | ||
import type TokenStorageInterface from './TokenStorageInterface'; | ||
import type TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface'; | ||
import type AsyncStorageInterface from './AsyncStorageInterface'; | ||
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, SdkMetadata }; | ||
export type { SerializerInterface, Token, SdkMetadata, RestClientSdkInterface, TokenStorageInterface, TokenGeneratorInterface, AsyncStorageInterface, }; |
import UnitOfWork from './UnitOfWork'; | ||
import Mapping from './Mapping'; | ||
import TokenStorage from './TokenStorage'; | ||
import TokenStorageInterface from './TokenStorageInterface'; | ||
import SerializerInterface from './serializer/SerializerInterface'; | ||
import AbstractClient from './client/AbstractClient'; | ||
import { Token } from './TokenGenerator/types'; | ||
declare type Config = { | ||
path: string; | ||
scheme: string; | ||
port?: number; | ||
segment?: string; | ||
authorizationType?: string; | ||
useDefaultParameters?: boolean; | ||
}; | ||
import type RestClientSdkInterface from './RestClientSdkInterface'; | ||
import type { Config } from './RestClientSdkInterface'; | ||
declare type Entity = any; | ||
@@ -21,10 +15,10 @@ export declare type MetadataDefinition = { | ||
export declare type SdkMetadata = Record<string, MetadataDefinition>; | ||
declare class RestClientSdk<M extends SdkMetadata> { | ||
declare class RestClientSdk<M extends SdkMetadata> implements RestClientSdkInterface<M> { | ||
#private; | ||
config: Config; | ||
tokenStorage: TokenStorage<Token>; | ||
tokenStorage: TokenStorageInterface<Token>; | ||
serializer: SerializerInterface; | ||
mapping: Mapping; | ||
unitOfWork: UnitOfWork; | ||
constructor(tokenStorage: TokenStorage<Token>, config: Config, mapping: Mapping, serializer?: SerializerInterface); | ||
constructor(tokenStorage: TokenStorageInterface<Token>, config: Config, mapping: Mapping, serializer?: SerializerInterface); | ||
/** | ||
@@ -31,0 +25,0 @@ * get a repository by it's metadata "key" attribute |
import TokenGeneratorInterface from './TokenGeneratorInterface'; | ||
import { Token } from './types'; | ||
interface ProvidedToken extends Token { | ||
access_token: string; | ||
token_type: string; | ||
refresh_token?: never; | ||
expires_in?: never; | ||
scope?: never; | ||
} | ||
declare type Parameters = { | ||
grant_type: 'provided'; | ||
}; | ||
declare type RefreshTokenFunc = () => Promise<ProvidedToken>; | ||
declare class ProvidedTokenGenerator implements TokenGeneratorInterface<ProvidedToken> { | ||
declare type RefreshTokenFunc = () => Promise<Token>; | ||
declare class ProvidedTokenGenerator implements TokenGeneratorInterface<Token> { | ||
#private; | ||
constructor(token: string, refreshTokenFunc?: null | RefreshTokenFunc); | ||
generateToken(parameters: Parameters): Promise<ProvidedToken>; | ||
refreshToken(accessToken: ProvidedToken): Promise<ProvidedToken>; | ||
autoGenerateToken(): Promise<ProvidedToken>; | ||
constructor(token: Token, refreshTokenFunc?: null | RefreshTokenFunc); | ||
generateToken(): Promise<Token>; | ||
refreshToken(accessToken: Token): Promise<Token>; | ||
autoGenerateToken(): Promise<Token>; | ||
} | ||
export default ProvidedTokenGenerator; |
@@ -8,4 +8,1 @@ export interface Token { | ||
} | ||
export interface TokenGeneratorParameters { | ||
grant_type: string; | ||
} |
import TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface'; | ||
import { Token, TokenGeneratorParameters } from './TokenGenerator/types'; | ||
import { Token } from './TokenGenerator/types'; | ||
import AsyncStorageInterface from './AsyncStorageInterface'; | ||
import type TokenStorageInterface from './TokenStorageInterface'; | ||
interface HasExpiresAt { | ||
expires_at: null | number; | ||
} | ||
declare class TokenStorage<T extends Token> { | ||
declare class TokenStorage<T extends Token> implements TokenStorageInterface<T> { | ||
#private; | ||
@@ -17,3 +18,3 @@ accessTokenKey: string; | ||
_addExpiresAtToResponseData(responseData: T, callTimestamp: number): T & HasExpiresAt; | ||
generateToken<G extends TokenGeneratorParameters>(parameters: G): Promise<T & HasExpiresAt>; | ||
generateToken(parameters: unknown): Promise<T & HasExpiresAt>; | ||
refreshToken(): Promise<T & HasExpiresAt>; | ||
@@ -20,0 +21,0 @@ /** |
{ | ||
"name": "rest-client-sdk", | ||
"version": "5.0.0-rc.7", | ||
"version": "5.0.0-rc.8", | ||
"description": "Rest Client SDK for API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import URI from 'urijs'; | ||
import { OauthError, getHttpErrorFromResponse } from '../ErrorFactory'; | ||
import TokenStorage from '../TokenStorage'; | ||
import TokenStorageInterface from '../TokenStorageInterface'; | ||
import { removeAuthorization, removeUndefinedHeaders } from './headerUtils'; | ||
@@ -18,3 +18,3 @@ // eslint-disable-next-line import/no-duplicates | ||
#tokenStorage: TokenStorage<Token>; | ||
#tokenStorage: TokenStorageInterface<Token>; | ||
@@ -350,7 +350,5 @@ serializer: SerializerInterface; | ||
if (this.#tokenStorage) { | ||
return Promise.all([ | ||
this.#tokenStorage.getCurrentTokenExpiresIn(), | ||
this.#tokenStorage.getAccessToken(), | ||
]) | ||
.then(([accessTokenExpiresIn, accessToken]) => { | ||
return this.#tokenStorage | ||
.getCurrentTokenExpiresIn() | ||
.then((accessTokenExpiresIn) => { | ||
if ( | ||
@@ -367,3 +365,3 @@ accessTokenExpiresIn !== null && | ||
return accessToken; | ||
return this.#tokenStorage.getAccessToken(); | ||
}) | ||
@@ -370,0 +368,0 @@ .then((token) => this._doFetch(token, input, requestParams)); |
@@ -28,2 +28,6 @@ import RestClientSdk from './RestClientSdk'; | ||
import type { SdkMetadata } from './RestClientSdk'; | ||
import type RestClientSdkInterface from './RestClientSdkInterface'; | ||
import type TokenStorageInterface from './TokenStorageInterface'; | ||
import type TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface'; | ||
import type AsyncStorageInterface from './AsyncStorageInterface'; | ||
@@ -54,2 +58,10 @@ export default RestClientSdk; | ||
}; | ||
export type { SerializerInterface, Token, SdkMetadata }; | ||
export type { | ||
SerializerInterface, | ||
Token, | ||
SdkMetadata, | ||
RestClientSdkInterface, | ||
TokenStorageInterface, | ||
TokenGeneratorInterface, | ||
AsyncStorageInterface, | ||
}; |
import JsSerializer from './serializer/JsSerializer'; | ||
import UnitOfWork from './UnitOfWork'; | ||
import Mapping from './Mapping'; | ||
import TokenStorage from './TokenStorage'; | ||
import TokenStorageInterface from './TokenStorageInterface'; | ||
import SerializerInterface from './serializer/SerializerInterface'; | ||
import AbstractClient from './client/AbstractClient'; | ||
import { Token } from './TokenGenerator/types'; | ||
// eslint-disable-next-line import/no-duplicates | ||
import type RestClientSdkInterface from './RestClientSdkInterface'; | ||
// eslint-disable-next-line import/no-duplicates | ||
import type { Config } from './RestClientSdkInterface'; | ||
type Config = { | ||
path: string; | ||
scheme: string; | ||
port?: number; | ||
segment?: string; | ||
authorizationType?: string; // default to "Bearer", but can be "Basic" or anything | ||
useDefaultParameters?: boolean; | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
@@ -31,6 +26,7 @@ type Entity = any; | ||
class RestClientSdk<M extends SdkMetadata> { | ||
class RestClientSdk<M extends SdkMetadata> | ||
implements RestClientSdkInterface<M> { | ||
config: Config; | ||
public tokenStorage: TokenStorage<Token>; | ||
public tokenStorage: TokenStorageInterface<Token>; | ||
@@ -46,3 +42,3 @@ public serializer: SerializerInterface; | ||
constructor( | ||
tokenStorage: TokenStorage<Token>, | ||
tokenStorage: TokenStorageInterface<Token>, | ||
config: Config, | ||
@@ -49,0 +45,0 @@ mapping: Mapping, |
@@ -5,3 +5,3 @@ /* eslint-disable camelcase */ | ||
import { memoizePromise } from '../decorator'; | ||
import { Token, TokenGeneratorParameters } from './types'; | ||
import { Token } from './types'; | ||
@@ -27,8 +27,7 @@ const ERROR_CONFIG_EMPTY = 'TokenGenerator config must be set'; | ||
type Parameters = TokenGeneratorParameters & | ||
BaseParameters & { | ||
grant_type: 'client_credentials'; | ||
client_id: string; | ||
client_secret: string; | ||
}; | ||
type Parameters = BaseParameters & { | ||
grant_type: 'client_credentials'; | ||
client_id: string; | ||
client_secret: string; | ||
}; | ||
@@ -35,0 +34,0 @@ interface ClientCredentialToken extends Token { |
@@ -5,22 +5,10 @@ /* eslint-disable camelcase */ | ||
interface ProvidedToken extends Token { | ||
access_token: string; | ||
token_type: string; | ||
refresh_token?: never; | ||
expires_in?: never; | ||
scope?: never; | ||
} | ||
type RefreshTokenFunc = () => Promise<Token>; | ||
type Parameters = { | ||
grant_type: 'provided'; | ||
}; | ||
class ProvidedTokenGenerator implements TokenGeneratorInterface<Token> { | ||
#token: Token; | ||
type RefreshTokenFunc = () => Promise<ProvidedToken>; | ||
class ProvidedTokenGenerator implements TokenGeneratorInterface<ProvidedToken> { | ||
#token: string; | ||
#refreshTokenFunc: null | RefreshTokenFunc; | ||
constructor(token: string, refreshTokenFunc: null | RefreshTokenFunc = null) { | ||
constructor(token: Token, refreshTokenFunc: null | RefreshTokenFunc = null) { | ||
this.#token = token; | ||
@@ -31,11 +19,8 @@ this.#refreshTokenFunc = refreshTokenFunc; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
generateToken(parameters: Parameters): Promise<ProvidedToken> { | ||
return Promise.resolve({ | ||
access_token: this.#token, | ||
token_type: 'bearer', | ||
}); | ||
generateToken(): Promise<Token> { | ||
return Promise.resolve(this.#token); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
refreshToken(accessToken: ProvidedToken): Promise<ProvidedToken> { | ||
refreshToken(accessToken: Token): Promise<Token> { | ||
if (typeof this.#refreshTokenFunc === 'function') { | ||
@@ -45,7 +30,7 @@ return this.#refreshTokenFunc(); | ||
return this.generateToken({ grant_type: 'provided' }); | ||
return this.generateToken(); | ||
} | ||
autoGenerateToken(): Promise<ProvidedToken> { | ||
return this.generateToken({ grant_type: 'provided' }); | ||
autoGenerateToken(): Promise<Token> { | ||
return this.generateToken(); | ||
} | ||
@@ -52,0 +37,0 @@ } |
@@ -9,4 +9,1 @@ /* eslint-disable camelcase */ | ||
} | ||
export interface TokenGeneratorParameters { | ||
grant_type: string; | ||
} |
/* eslint-disable camelcase */ | ||
import TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface'; | ||
import { Token, TokenGeneratorParameters } from './TokenGenerator/types'; | ||
import { Token } from './TokenGenerator/types'; | ||
import AsyncStorageInterface from './AsyncStorageInterface'; | ||
import type TokenStorageInterface from './TokenStorageInterface'; | ||
@@ -10,3 +11,3 @@ interface HasExpiresAt { | ||
class TokenStorage<T extends Token> { | ||
class TokenStorage<T extends Token> implements TokenStorageInterface<T> { | ||
#tokenGenerator: TokenGeneratorInterface<T>; | ||
@@ -18,3 +19,3 @@ | ||
#asyncStorage: AsyncStorageInterface; | ||
#asyncStorage!: AsyncStorageInterface; | ||
@@ -28,3 +29,3 @@ constructor( | ||
this.#hasATokenBeenGenerated = false; | ||
this.#asyncStorage = asyncStorage; // should be `setAsyncStorage(asyncStorage)` but TS mark this an an error | ||
this.setAsyncStorage(asyncStorage); | ||
this.accessTokenKey = accessTokenKey; | ||
@@ -81,2 +82,6 @@ } | ||
): T & HasExpiresAt { | ||
if (!responseData) { | ||
return responseData; | ||
} | ||
const updatedResponseData: T & HasExpiresAt = { | ||
@@ -99,6 +104,3 @@ ...responseData, | ||
generateToken<G extends TokenGeneratorParameters>( | ||
parameters: G | ||
): Promise<T & HasExpiresAt> { | ||
this.#hasATokenBeenGenerated = true; | ||
generateToken(parameters: unknown): Promise<T & HasExpiresAt> { | ||
const callTimestamp = Date.now(); | ||
@@ -114,5 +116,6 @@ | ||
return this._storeAccessToken(updatedResponseData).then( | ||
() => updatedResponseData | ||
); | ||
return this._storeAccessToken(updatedResponseData).then(() => { | ||
this.#hasATokenBeenGenerated = true; | ||
return updatedResponseData; | ||
}); | ||
}); | ||
@@ -119,0 +122,0 @@ } |
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
58
525796
6829