Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(javascript): simplify transporter.request #617

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
StackFrame,
TransporterOptions,
Transporter,
Headers,
QueryParameters,
} from '../types';

Expand Down Expand Up @@ -99,7 +98,7 @@ export function createTransporter({
const method = request.method;

// On `GET`, the data is proxied to query parameters.
const dataQueryParameters: Record<string, any> =
const dataQueryParameters: QueryParameters =
request.method === 'GET'
? {
...request.data,
Expand Down Expand Up @@ -238,10 +237,6 @@ export function createTransporter({

function createRequest<TResponse>(
baseRequest: Request,
methodOptions: {
headers: Headers;
queryParameters: QueryParameters;
},
baseRequestOptions?: RequestOptions
): Promise<TResponse> {
const mergedData: Request['data'] = Array.isArray(baseRequest.data)
Expand All @@ -258,12 +253,12 @@ export function createTransporter({
cacheable: baseRequestOptions?.cacheable,
timeout: baseRequestOptions?.timeout,
queryParameters: {
...methodOptions.queryParameters,
...baseRequest.queryParameters,
...baseRequestOptions?.queryParameters,
},
headers: {
Accept: 'application/json',
...methodOptions.headers,
...baseRequest.headers,
...baseRequestOptions?.headers,
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Headers } from './Transporter';
import type { Headers, QueryParameters } from './Transporter';

export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';

export type Request = {
method: Method;
path: string;
queryParameters: QueryParameters;
data?: Array<Record<string, any>> | Record<string, any>;
headers: Headers;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the headers are in both the EndRequest and the Request now, but the request is not used by the http requester, only the echoRequester so it's okay but we must not get confused in the createEchoRequester, where we want the final headers and not the one from request.

cacheable?: boolean;
/**
* Some POST methods in the Algolia REST API uses the `read` transporter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@ export type Transporter = {
*/
request: <TResponse>(
baseRequest: Request,
methodOptions: {
headers: Headers;
queryParameters: QueryParameters;
},
baseRequestOptions?: RequestOptions
) => Promise<TResponse>;
};
7 changes: 3 additions & 4 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
const request: Request = {
method: '{{httpMethod}}',
path: requestPath,
queryParameters,
headers,
{{#bodyParam}}
data: {{paramName}},
{{/bodyParam}}
Expand All @@ -226,10 +228,7 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
{{/vendorExtensions.x-cacheable}}
};

return transporter.request(request, {
queryParameters,
headers,
}, requestOptions);
return transporter.request(request, requestOptions);
},

{{/operation}}
Expand Down