-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): provide a way to request a custom URL
feat(sdk): add apiName in the request plugin context
- Loading branch information
Showing
10 changed files
with
199 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { type Api, type ApiClient, ApiTypes, type RequestOptionsParameters, type ReviverType } from '../fwk'; | ||
|
||
/** | ||
* Generic request to the API | ||
*/ | ||
export interface GenericRequestOptions<T> extends Omit<RequestOptionsParameters, 'api' | 'headers'> { | ||
/** API used to identify the call */ | ||
api?: RequestOptionsParameters['api']; | ||
/** Custom headers to provide to the request */ | ||
headers?: RequestOptionsParameters['headers']; | ||
/** Custom operation ID to identify the request */ | ||
operationId?: string; | ||
/** Custom reviver to revive the response of the call */ | ||
revivers?: ReviverType<T> | undefined | { [key: number]: ReviverType<T> | undefined }; | ||
} | ||
|
||
export class GenericApi implements Api { | ||
/** API name */ | ||
public static readonly apiName = 'GenericApi'; | ||
|
||
/** @inheritDoc */ | ||
public readonly apiName = GenericApi.apiName; | ||
|
||
|
||
/** @inheritDoc */ | ||
public client: ApiClient; | ||
|
||
/** | ||
* Initialize your interface | ||
* @param apiClient | ||
* @params apiClient Client used to process call to the API | ||
*/ | ||
constructor(apiClient: ApiClient) { | ||
this.client = apiClient; | ||
} | ||
|
||
/** | ||
* Process to request to the API in the context of the SDK | ||
* @param requestOptions Option to provide to process to the call | ||
*/ | ||
public async request<T>(requestOptions: GenericRequestOptions<T>): Promise<T> { | ||
const metadataHeaderAccept = requestOptions.metadata?.headerAccept || 'application/json'; | ||
const headers: { [key: string]: string | undefined } = { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'Content-Type': requestOptions.metadata?.headerContentType || 'application/json', | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
...(metadataHeaderAccept ? { 'Accept': metadataHeaderAccept } : {}) | ||
}; | ||
|
||
const requestParameters: RequestOptionsParameters = { | ||
api: this, | ||
headers, | ||
...requestOptions | ||
}; | ||
const options = this.client.getRequestOptions ? | ||
await this.client.getRequestOptions(requestParameters) : | ||
await this.client.prepareOptions(requestParameters.basePath, requestParameters.method, requestParameters.queryParams || {}, requestParameters.headers); | ||
const url = this.client.prepareUrl(options.basePath, options.queryParams); | ||
|
||
const ret = this.client.processCall<T>(url, options, ApiTypes.DEFAULT, requestOptions.api!.apiName, requestOptions.revivers, requestOptions.operationId); | ||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters