Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Dec 20, 2024
1 parent f734722 commit ee314d9
Show file tree
Hide file tree
Showing 82 changed files with 8,306 additions and 7,505 deletions.
511 changes: 258 additions & 253 deletions packages/fdr-sdk/src/api-definition/__test__/join.test.ts

Large diffs are not rendered by default.

420 changes: 210 additions & 210 deletions packages/fdr-sdk/src/api-definition/__test__/prune.test.ts

Large diffs are not rendered by default.

1,408 changes: 750 additions & 658 deletions packages/fdr-sdk/src/api-definition/migrators/v1ToV2.ts

Large diffs are not rendered by default.

274 changes: 147 additions & 127 deletions packages/fdr-sdk/src/api-definition/snippets/SnippetHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,156 +5,176 @@ import urljoin from "url-join";
import type * as Latest from "../latest";

interface SnippetHttpRequestBodyJson {
type: "json";
value?: unknown;
type: "json";
value?: unknown;
}

export interface SnippetHttpRequestBodyForm {
type: "form";
value: Record<string, SnippetHttpRequestBodyFormValue>;
type: "form";
value: Record<string, SnippetHttpRequestBodyFormValue>;
}

export interface SnippetHttpRequestBodyFormValueFilename {
type: "filename";
filename: string;
contentType: string | undefined;
type: "filename";
filename: string;
contentType: string | undefined;
}

export interface SnippetHttpRequestBodyFormValueFilenames {
type: "filenames";
files: Omit<SnippetHttpRequestBodyFormValueFilename, "type">[];
type: "filenames";
files: Omit<SnippetHttpRequestBodyFormValueFilename, "type">[];
}

export type SnippetHttpRequestBodyFormValue =
| SnippetHttpRequestBodyJson
| SnippetHttpRequestBodyFormValueFilename
| SnippetHttpRequestBodyFormValueFilenames;
| SnippetHttpRequestBodyJson
| SnippetHttpRequestBodyFormValueFilename
| SnippetHttpRequestBodyFormValueFilenames;

interface SnippetHttpRequestBodyBytes {
type: "bytes";
filename: string;
type: "bytes";
filename: string;
}

export type SnippetHttpRequestBody =
| SnippetHttpRequestBodyJson
| SnippetHttpRequestBodyForm
| SnippetHttpRequestBodyBytes;
| SnippetHttpRequestBodyJson
| SnippetHttpRequestBodyForm
| SnippetHttpRequestBodyBytes;

export interface SnippetHttpRequest {
method: string;
url: string;
searchParams: Record<string, unknown>;
headers: Record<string, unknown>;
basicAuth?: {
username: string;
password: string;
};
body: SnippetHttpRequestBody | undefined;
method: string;
url: string;
searchParams: Record<string, unknown>;
headers: Record<string, unknown>;
basicAuth?: {
username: string;
password: string;
};
body: SnippetHttpRequestBody | undefined;
}

// TODO: validate that global headers are also included in the example by CLI or FDR
export function toSnippetHttpRequest(
endpoint: Latest.EndpointDefinition,
example: Latest.ExampleEndpointCall,
auth: Latest.AuthScheme | undefined,
endpoint: Latest.EndpointDefinition,
example: Latest.ExampleEndpointCall,
auth: Latest.AuthScheme | undefined
): SnippetHttpRequest[] {
const snippets: SnippetHttpRequest[] = [];
for (const request of endpoint.requests ?? []) {
const environmentUrl = (
endpoint.environments?.find((env) => env.id === endpoint.defaultEnvironment) ?? endpoint.environments?.[0]
)?.baseUrl;
const url = urljoin(compact([environmentUrl, example.path]));

const headers: Record<string, unknown> = { ...example.headers };

let basicAuth: { username: string; password: string } | undefined;

if (endpoint.auth && endpoint.auth.length > 0 && auth) {
visitDiscriminatedUnion(auth, "type")._visit({
basicAuth: ({ usernameName = "username", passwordName = "password" }) => {
basicAuth = { username: `<${usernameName}>`, password: `<${passwordName}>` };
},
bearerAuth: ({ tokenName = "token" }) => {
headers.Authorization = `Bearer <${tokenName}>`;
},
header: ({ headerWireValue, nameOverride = headerWireValue, prefix }) => {
headers[headerWireValue] = prefix != null ? `${prefix} <${nameOverride}>` : `<${nameOverride}>`;
},
oAuth: ({ value: clientCredentials }) => {
visitDiscriminatedUnion(clientCredentials, "type")._visit({
clientCredentials: () => {
headers.Authorization = "Bearer <token>";
},
_other: noop,
});
},
_other: noop,
});
}

const body: Latest.ExampleEndpointRequest | undefined = example.requestBody;

if (request?.contentType != null) {
headers["Content-Type"] = request?.contentType;
}

if (body != null && headers["Content-Type"] == null) {
if (body.type === "json") {
headers["Content-Type"] = "application/json";
} else if (body.type === "form") {
headers["Content-Type"] = "multipart/form-data";
}
}

snippets.push({
method: endpoint.method,
url,
searchParams: example.queryParameters ?? {},
headers: JSON.parse(JSON.stringify(headers)),
basicAuth,
body:
body == null
? undefined
: visitDiscriminatedUnion(body)._visit<SnippetHttpRequestBody | undefined>({
json: (value) => value,
form: (value) => {
const toRet: Record<string, SnippetHttpRequestBodyFormValue> = {};
for (const [key, val] of Object.entries(value.value)) {
toRet[key] = visitDiscriminatedUnion(val)._visit<SnippetHttpRequestBodyFormValue>({
json: (value) => value,
filename: (value) => ({
type: "filename",
filename: value.value,
contentType: undefined, // TODO: infer content type?
}),
filenames: (value) => ({
type: "filenames",
files: value.value.map((filename) => ({
filename,
contentType: undefined, // TODO: infer content type?
})),
}),
filenameWithData: (value) => ({
type: "filename",
filename: value.filename,
contentType: undefined, // TODO: infer content type?
}),
filenamesWithData: (value) => ({
type: "filenames",
files: value.value.map(({ filename }) => ({
filename,
contentType: undefined, // TODO: infer content type?
})),
}),
});
}
return { type: "form", value: toRet };
},
// TODO: filename should be provided in the example from the API definition
bytes: () => ({ type: "bytes", filename: "<filename>" }),
_other: () => undefined,
}),
});
const snippets: SnippetHttpRequest[] = [];
for (const request of endpoint.requests ?? []) {
const environmentUrl = (
endpoint.environments?.find(
(env) => env.id === endpoint.defaultEnvironment
) ?? endpoint.environments?.[0]
)?.baseUrl;
const url = urljoin(compact([environmentUrl, example.path]));

const headers: Record<string, unknown> = { ...example.headers };

let basicAuth: { username: string; password: string } | undefined;

if (endpoint.auth && endpoint.auth.length > 0 && auth) {
visitDiscriminatedUnion(auth, "type")._visit({
basicAuth: ({
usernameName = "username",
passwordName = "password",
}) => {
basicAuth = {
username: `<${usernameName}>`,
password: `<${passwordName}>`,
};
},
bearerAuth: ({ tokenName = "token" }) => {
headers.Authorization = `Bearer <${tokenName}>`;
},
header: ({
headerWireValue,
nameOverride = headerWireValue,
prefix,
}) => {
headers[headerWireValue] =
prefix != null
? `${prefix} <${nameOverride}>`
: `<${nameOverride}>`;
},
oAuth: ({ value: clientCredentials }) => {
visitDiscriminatedUnion(clientCredentials, "type")._visit({
clientCredentials: () => {
headers.Authorization = "Bearer <token>";
},
_other: noop,
});
},
_other: noop,
});
}
return snippets;

const body: Latest.ExampleEndpointRequest | undefined = example.requestBody;

if (request?.contentType != null) {
headers["Content-Type"] = request?.contentType;
}

if (body != null && headers["Content-Type"] == null) {
if (body.type === "json") {
headers["Content-Type"] = "application/json";
} else if (body.type === "form") {
headers["Content-Type"] = "multipart/form-data";
}
}

snippets.push({
method: endpoint.method,
url,
searchParams: example.queryParameters ?? {},
headers: JSON.parse(JSON.stringify(headers)),
basicAuth,
body:
body == null
? undefined
: visitDiscriminatedUnion(body)._visit<
SnippetHttpRequestBody | undefined
>({
json: (value) => value,
form: (value) => {
const toRet: Record<string, SnippetHttpRequestBodyFormValue> =
{};
for (const [key, val] of Object.entries(value.value)) {
toRet[key] = visitDiscriminatedUnion(
val
)._visit<SnippetHttpRequestBodyFormValue>({
json: (value) => value,
filename: (value) => ({
type: "filename",
filename: value.value,
contentType: undefined, // TODO: infer content type?
}),
filenames: (value) => ({
type: "filenames",
files: value.value.map((filename) => ({
filename,
contentType: undefined, // TODO: infer content type?
})),
}),
filenameWithData: (value) => ({
type: "filename",
filename: value.filename,
contentType: undefined, // TODO: infer content type?
}),
filenamesWithData: (value) => ({
type: "filenames",
files: value.value.map(({ filename }) => ({
filename,
contentType: undefined, // TODO: infer content type?
})),
}),
});
}
return { type: "form", value: toRet };
},
// TODO: filename should be provided in the example from the API definition
bytes: () => ({ type: "bytes", filename: "<filename>" }),
_other: () => undefined,
}),
});
}
return snippets;
}
Loading

0 comments on commit ee314d9

Please sign in to comment.