Skip to content

Commit

Permalink
Merge branch 'master' into make-v22
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Jan 1, 2025
2 parents 4d51d3a + 4903a8e commit 78b3ba0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 43 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

## Version 21

### v21.10.0

- New `Integration` option: `serverUrl`, string, optional, the API URL for the generated client:
- Currently used for generating example implementation;
- Default value remains `https://example.com`;
- Using `new URL()` for constructing the final request URL in the example implementation of the generated client:
- That enables handling `serverUrl` both with and without trailing slash;

### v21.9.0

- Deprecating `MethodPath` type in the code generated by `Integration`:
Expand Down
13 changes: 8 additions & 5 deletions example/example.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,14 @@ export const exampleImplementation: Implementation = async (
) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : `?${new URLSearchParams(params)}`;
const response = await fetch(`https://example.com${path}${searchParams}`, {
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const response = await fetch(
new URL(`${path}${searchParams}`, "https://example.com"),
{
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
},
);
const contentType = response.headers.get("content-type");
if (!contentType) return;
const isJSON = contentType.startsWith("application/json");
Expand Down
20 changes: 15 additions & 5 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ interface IntegrationParams {
* @default "client"
* */
variant?: "types" | "client";
/**
* @desc The API URL to use in the generated code
* @default https://example.com
* */
serverUrl?: string;
/**
* @desc configures the style of object's optional properties
* @default { withQuestionMark: true, withUndefined: true }
Expand Down Expand Up @@ -163,6 +168,7 @@ export class Integration {
routing,
brandHandling,
variant = "client",
serverUrl = "https://example.com",
optionalPropStyle = { withQuestionMark: true, withUndefined: true },
noContent = z.undefined(),
}: IntegrationParams) {
Expand Down Expand Up @@ -461,15 +467,19 @@ export class Integration {
),
);

// const response = await fetch(`https://example.com${path}${searchParams}`, { ___ });
// const response = await fetch(new URL(`${path}${searchParams}`, "https://example.com"), { ___ });
const responseStatement = makeConst(
this.ids.responseConst,
f.createAwaitExpression(
f.createCallExpression(f.createIdentifier(fetch.name), undefined, [
makeTemplate(
"https://example.com",
[this.ids.pathParameter],
[this.ids.searchParamsConst],
makeNew(
f.createIdentifier(URL.name),
makeTemplate(
"",
[this.ids.pathParameter],
[this.ids.searchParamsConst],
),
f.createStringLiteral(serverUrl),
),
f.createObjectLiteralExpression([
methodProperty,
Expand Down
15 changes: 7 additions & 8 deletions tests/system/example.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,14 @@ describe("Example", async () => {
const createDefaultImplementation =
(host: string): Implementation =>
async (method, path, params) => {
const searchParams =
method === "get" ? `?${new URLSearchParams(params)}` : "";
const response = await fetch(`${host}${path}${searchParams}`, {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : `?${new URLSearchParams(params)}`;
const response = await fetch(new URL(`${path}${searchParams}`, host), {
method: method.toUpperCase(),
headers:
method === "get"
? undefined
: { "Content-Type": "application/json", token: "456" },
body: method === "get" ? undefined : JSON.stringify(params),
headers: hasBody
? { "Content-Type": "application/json", token: "456" }
: undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const contentType = response.headers.get("content-type");
if (!contentType) return;
Expand Down
65 changes: 40 additions & 25 deletions tests/unit/__snapshots__/integration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ export const exampleImplementation: Implementation = async (
) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : \`?\${new URLSearchParams(params)}\`;
const response = await fetch(\`https://example.com\${path}\${searchParams}\`, {
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const response = await fetch(
new URL(\`\${path}\${searchParams}\`, "https://example.com"),
{
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
},
);
const contentType = response.headers.get("content-type");
if (!contentType) return;
const isJSON = contentType.startsWith("application/json");
Expand Down Expand Up @@ -232,11 +235,14 @@ export const exampleImplementation: Implementation = async (
) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : \`?\${new URLSearchParams(params)}\`;
const response = await fetch(\`https://example.com\${path}\${searchParams}\`, {
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const response = await fetch(
new URL(\`\${path}\${searchParams}\`, "https://example.com"),
{
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
},
);
const contentType = response.headers.get("content-type");
if (!contentType) return;
const isJSON = contentType.startsWith("application/json");
Expand Down Expand Up @@ -356,11 +362,14 @@ export const exampleImplementation: Implementation = async (
) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : \`?\${new URLSearchParams(params)}\`;
const response = await fetch(\`https://example.com\${path}\${searchParams}\`, {
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const response = await fetch(
new URL(\`\${path}\${searchParams}\`, "https://example.com"),
{
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
},
);
const contentType = response.headers.get("content-type");
if (!contentType) return;
const isJSON = contentType.startsWith("application/json");
Expand Down Expand Up @@ -898,11 +907,14 @@ export const exampleImplementation: Implementation = async (
) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : \`?\${new URLSearchParams(params)}\`;
const response = await fetch(\`https://example.com\${path}\${searchParams}\`, {
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const response = await fetch(
new URL(\`\${path}\${searchParams}\`, "https://example.com"),
{
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
},
);
const contentType = response.headers.get("content-type");
if (!contentType) return;
const isJSON = contentType.startsWith("application/json");
Expand Down Expand Up @@ -1501,11 +1513,14 @@ export const exampleImplementation: Implementation = async (
) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : \`?\${new URLSearchParams(params)}\`;
const response = await fetch(\`https://example.com\${path}\${searchParams}\`, {
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
});
const response = await fetch(
new URL(\`\${path}\${searchParams}\`, "https://example.com"),
{
method: method.toUpperCase(),
headers: hasBody ? { "Content-Type": "application/json" } : undefined,
body: hasBody ? JSON.stringify(params) : undefined,
},
);
const contentType = response.headers.get("content-type");
if (!contentType) return;
const isJSON = contentType.startsWith("application/json");
Expand Down

0 comments on commit 78b3ba0

Please sign in to comment.