Skip to content

Commit

Permalink
try removing cross product from converter
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Dec 20, 2024
1 parent 0551cae commit e730db8
Showing 1 changed file with 70 additions and 63 deletions.
133 changes: 70 additions & 63 deletions packages/fdr-sdk/src/api-definition/migrators/v1ToV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,71 +407,78 @@ export class ApiDefinitionV1ToLatest {
if (examples.length === 0) {
return undefined;
}
// We take the cross product of requests and responses
return endpoint.responses?.flatMap((response) => {
return (endpoint.requests ?? []).flatMap((request) =>
examples.map((example): V2.ExampleEndpointCall => {
const toRet: V2.ExampleEndpointCall = {
path: example.path,
responseStatusCode: example.responseStatusCode,
name: example.name,
description: example.description,
pathParameters: example.pathParameters,
queryParameters: example.queryParameters,
headers: example.headers,
requestBody: example.requestBodyV3,
responseBody: example.responseBodyV3,
snippets: undefined,
};

if (example.requestBodyV3) {
toRet.requestBody = visitDiscriminatedUnion(
example.requestBodyV3,
)._visit<APIV1Read.ExampleEndpointRequest>({
bytes: (value) => value,
json: (value) => ({
type: "json",
value: sortKeysByShape(value.value, request.body, this.types),
}),
form: (value) => ({
type: "form",
value: mapValues(value.value, (formValue, key): APIV1Read.FormValue => {
if (formValue.type === "json") {
const shape =
request.body.type === "formData"
? request.body.fields.find(
(field): field is V2.FormDataField.Property =>
field.key === key && field.type === "property",
)?.valueShape
: undefined;
return {
type: "json",
value: sortKeysByShape(formValue.value, shape, this.types),
};
} else {
return formValue;
}
}),
}),
});
}

if (toRet.responseBody) {
toRet.responseBody.value = sortKeysByShape(toRet.responseBody.value, response.body, this.types);
}

toRet.snippets = this.migrateEndpointSnippets(
endpoint,
toRet,
example.codeSamples,
example.codeExamples,
this.flags,
);

return toRet;
}),

examples.map((example): V2.ExampleEndpointCall => {
const toRet: V2.ExampleEndpointCall = {
path: example.path,
responseStatusCode: example.responseStatusCode,
name: example.name,
description: example.description,
pathParameters: example.pathParameters,
queryParameters: example.queryParameters,
headers: example.headers,
requestBody: example.requestBodyV3,
responseBody: example.responseBodyV3,
snippets: undefined,
};

if (example.requestBodyV3) {
toRet.requestBody = visitDiscriminatedUnion(
example.requestBodyV3,
)._visit<APIV1Read.ExampleEndpointRequest>({
bytes: (value) => value,
json: (value) => ({
type: "json",
value: sortKeysByShape(value.value, endpoint.requests?.[0]?.body, this.types),
}),
form: (value) => ({
type: "form",
value: mapValues(value.value, (formValue, key): APIV1Read.FormValue => {
if (formValue.type === "json") {
const shape =
endpoint.requests?.[0]?.body.type === "formData"
? endpoint.requests?.[0]?.body.fields.find(
(field): field is V2.FormDataField.Property =>
field.key === key && field.type === "property",
)?.valueShape
: undefined;
return {
type: "json",
value: sortKeysByShape(formValue.value, shape, this.types),
};
} else {
return formValue;
}
}),
}),
});
}

if (toRet.responseBody) {
toRet.responseBody.value = sortKeysByShape(
toRet.responseBody.value,
endpoint.responses?.[0]?.body,
this.types,
);
}

toRet.snippets = this.migrateEndpointSnippets(
endpoint,
toRet,
example.codeSamples,
example.codeExamples,
this.flags,
);

return toRet;
});

// // We take the cross product of requests and responses
// return endpoint.responses?.flatMap((response) => {
// return (endpoint.requests ?? []).flatMap((request) =>
// {}
// );
// });
};

migrateHttpErrors = (errors: APIV1Read.ErrorDeclarationV2[] | undefined): V2.ErrorResponse[] | undefined => {
Expand Down

0 comments on commit e730db8

Please sign in to comment.