-
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 and add apiName in r…
…equest plugin context (#1578) ## Proposed change feat(sdk): provide a way to request a custom URL feat(sdk): add apiName in the request plugin context The purpose of this PR is to offer a way to do manually a request in the context of the SDK and benefit of the plugins register. To facilitate the identification of the request in the requestPlugin (as done in the replyPlugin), the context is provided to the request plugin. <!-- Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that is required for this change. -->
- Loading branch information
Showing
19 changed files
with
364 additions
and
355 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,66 @@ | ||
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 }; | ||
} | ||
|
||
/** | ||
* Generic request to the API | ||
*/ | ||
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
Oops, something went wrong.