Skip to content

Commit

Permalink
fix(misskey-js): content-typeはapplication/jsonでないもののみを記録するように
Browse files Browse the repository at this point in the history
  • Loading branch information
kakkokari-gtyih committed Sep 6, 2024
1 parent 74c93fc commit f02f28b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 393 deletions.
21 changes: 10 additions & 11 deletions packages/misskey-js/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,11 @@ async function generateEndpoints(
endpoint.request = req;

const reqType = new EndpointReqMediaType(path, req);
endpointReqMediaTypesSet.add(reqType.getMediaType());
endpointReqMediaTypes.push(reqType);
} else {
endpointReqMediaTypesSet.add('application/json');
endpointReqMediaTypes.push(new EndpointReqMediaType(path, undefined, 'application/json'));
if (reqType.getMediaType() !== 'application/json') {
endpointReqMediaTypesSet.add(reqType.getMediaType());
endpointReqMediaTypes.push(reqType);
}
}
} else {
endpointReqMediaTypesSet.add('application/json');
endpointReqMediaTypes.push(new EndpointReqMediaType(path, undefined, 'application/json'));
}

if (operation.responses && isResponseObject(operation.responses['200']) && operation.responses['200'].content) {
Expand Down Expand Up @@ -158,16 +154,19 @@ async function generateEndpoints(
endpointOutputLine.push('');

function generateEndpointReqMediaTypesType() {
return `Record<keyof Endpoints, ${[...endpointReqMediaTypesSet].map((t) => `'${t}'`).join(' | ')}>`;
return `{ [K in keyof Endpoints]?: ${[...endpointReqMediaTypesSet].map((t) => `'${t}'`).join(' | ')}; }`;
}

endpointOutputLine.push(`export const endpointReqTypes: ${generateEndpointReqMediaTypesType()} = {`);
endpointOutputLine.push(`/**
* NOTE: The content-type for all endpoints not listed here is application/json.
*/`);
endpointOutputLine.push('export const endpointReqTypes = {');

endpointOutputLine.push(
...endpointReqMediaTypes.map(it => '\t' + it.toLine()),
);

endpointOutputLine.push('};');
endpointOutputLine.push(`} as const satisfies ${generateEndpointReqMediaTypesType()};`);
endpointOutputLine.push('');

await writeFile(endpointOutputPath, endpointOutputLine.join('\n'));
Expand Down
9 changes: 7 additions & 2 deletions packages/misskey-js/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ export class APIClient {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
}

private assertSpecialEpReqType(ep: keyof Endpoints): ep is keyof typeof endpointReqTypes {
return ep in endpointReqTypes;
}

public request<E extends keyof Endpoints, P extends Endpoints[E]['req']>(
endpoint: E,
params: P = {} as P,
credential?: string | null,
): Promise<SwitchCaseResponseType<E, P>> {
return new Promise((resolve, reject) => {
let mediaType = 'application/json';
if (endpoint in endpointReqTypes) {
if (this.assertSpecialEpReqType(endpoint) && endpointReqTypes[endpoint] != null) {

Check warning on line 70 in packages/misskey-js/src/api.ts

View workflow job for this annotation

GitHub Actions / lint (misskey-js)

Unnecessary conditional, both sides of the expression are literal values
mediaType = endpointReqTypes[endpoint];
}

let payload: FormData | string = '{}';

if (mediaType === 'application/json') {
Expand Down Expand Up @@ -100,7 +105,7 @@ export class APIClient {
method: 'POST',
body: payload,
headers: {
'Content-Type': endpointReqTypes[endpoint],
'Content-Type': mediaType,
},
credentials: 'omit',
cache: 'no-cache',
Expand Down
Loading

0 comments on commit f02f28b

Please sign in to comment.