Skip to content

Commit

Permalink
Renaming ExpressZodAPIClient to just Client (#2313)
Browse files Browse the repository at this point in the history
1. Since I also wanna make subscription class in #2280 
2. In the era of modules there is no longer need to have a unique name
  • Loading branch information
RobinTail authored Jan 14, 2025
1 parent e1b1e71 commit 88a0a66
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- `BuiltinLogger::profile()` behavior changed for picoseconds: expressing them through nanoseconds;
- Changes to client generated by `Integration`:
- The `splitResponse` property on the constructor argument is removed;
- The overload of `ExpressZodAPIClient::provide()` having 3 arguments and the `Provider` type are removed;
- The class name changed from `ExpressZodAPIClient` to just `Client`;
- The overload of the `Client::provide()` having 3 arguments and the `Provider` type are removed;
- The public `jsonEndpoints` const is removed — use the `content-type` header of an actual response instead;
- The public type `MethodPath` is removed — use the `Request` type instead.
- The approach on tagging endpoints changed:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1264,9 +1264,9 @@ Consuming the generated client requires Typescript version 4.1 or higher.

```typescript
// example frontend, simple implementation based on fetch()
import { ExpressZodAPIClient } from "./client.ts"; // the generated file
import { Client } from "./client.ts"; // the generated file

const client = new ExpressZodAPIClient(async (method, path, params) => {
const client = new Client(async (method, path, params) => {
const hasBody = !["get", "delete"].includes(method);
const searchParams = hasBody ? "" : `?${new URLSearchParams(params)}`;
const response = await fetch(`https://example.com${path}${searchParams}`, {
Expand Down
4 changes: 2 additions & 2 deletions example/example.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export type Implementation = (
params: Record<string, any>,
) => Promise<any>;

export class ExpressZodAPIClient {
export class Client {
public constructor(protected readonly implementation: Implementation) {}
public provide<K extends Request>(
request: K,
Expand Down Expand Up @@ -467,6 +467,6 @@ export const exampleImplementation: Implementation = async (
const isJSON = contentType.startsWith("application/json");
return response[isJSON ? "json" : "text"]();
};
const client = new ExpressZodAPIClient(exampleImplementation);
const client = new Client(exampleImplementation);
client.provide("get /v1/user/retrieve", { id: "10" });
*/
2 changes: 1 addition & 1 deletion src/integration-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export abstract class IntegrationBase {
protected ids = {
pathType: f.createIdentifier("Path"),
implementationType: f.createIdentifier("Implementation"),
clientClass: f.createIdentifier("ExpressZodAPIClient"),
clientClass: f.createIdentifier("Client"),
keyParameter: f.createIdentifier("key"),
pathParameter: f.createIdentifier("path"),
paramsArgument: f.createIdentifier("params"),
Expand Down
15 changes: 15 additions & 0 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Queries {
newDocs: TSESTree.ObjectExpression;
newFactory: TSESTree.Property & { key: TSESTree.Identifier };
newSSE: TSESTree.Property & { key: TSESTree.Identifier };
newClient: TSESTree.NewExpression;
}

type Listener = keyof Queries;
Expand All @@ -48,6 +49,7 @@ const queries: Record<Listener, string> = {
newSSE:
`${NT.NewExpression}[callee.name='EventStreamFactory'] > ` +
`${NT.ObjectExpression} > ${NT.Property}[key.name='events']`,
newClient: `${NT.NewExpression}[callee.name='ExpressZodAPIClient']`,
};

const listen = <
Expand Down Expand Up @@ -161,6 +163,19 @@ const v22 = ESLintUtils.RuleCreator.withoutDocs({
fix: (fixer) =>
fixer.replaceText(node.parent, ctx.sourceCode.getText(node.value)),
}),
newClient: (node) => {
const replacement = "Client";
ctx.report({
messageId: "change",
node: node.callee,
data: {
subject: "class",
from: "ExpressZodAPIClient",
to: replacement,
},
fix: (fixer) => fixer.replaceText(node.callee, replacement),
});
},
}),
});

Expand Down
11 changes: 3 additions & 8 deletions tests/system/example.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import assert from "node:assert/strict";
import { EventSource } from "undici";
import { spawn } from "node:child_process";
import { createReadStream, readFileSync } from "node:fs";
import {
ExpressZodAPIClient,
Implementation,
} from "../../example/example.client";
import { Client, Implementation } from "../../example/example.client";
import { givePort } from "../helpers";
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
Expand Down Expand Up @@ -445,7 +442,7 @@ describe("Example", async () => {
});

describe("Client", () => {
const createDefaultImplementation =
const createImplementation =
(host: string): Implementation =>
async (method, path, params) => {
const hasBody = !["get", "delete"].includes(method);
Expand All @@ -463,9 +460,7 @@ describe("Example", async () => {
return response[isJSON ? "json" : "text"]();
};

const client = new ExpressZodAPIClient(
createDefaultImplementation(`http://localhost:${port}`),
);
const client = new Client(createImplementation(`http://localhost:${port}`));

test("Should perform the request with a positive response", async () => {
const response = await client.provide("get /v1/user/retrieve", {
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/__snapshots__/integration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type Implementation = (
params: Record<string, any>,
) => Promise<any>;
export class ExpressZodAPIClient {
export class Client {
public constructor(protected readonly implementation: Implementation) {}
public provide<K extends Request>(
request: K,
Expand Down Expand Up @@ -118,7 +118,7 @@ export const exampleImplementation: Implementation = async (
const isJSON = contentType.startsWith("application/json");
return response[isJSON ? "json" : "text"]();
};
const client = new ExpressZodAPIClient(exampleImplementation);
const client = new Client(exampleImplementation);
client.provide("get /v1/user/retrieve", { id: "10" });
*/
"
Expand Down Expand Up @@ -209,7 +209,7 @@ export type Implementation = (
params: Record<string, any>,
) => Promise<any>;
export class ExpressZodAPIClient {
export class Client {
public constructor(protected readonly implementation: Implementation) {}
public provide<K extends Request>(
request: K,
Expand Down Expand Up @@ -242,7 +242,7 @@ export const exampleImplementation: Implementation = async (
const isJSON = contentType.startsWith("application/json");
return response[isJSON ? "json" : "text"]();
};
const client = new ExpressZodAPIClient(exampleImplementation);
const client = new Client(exampleImplementation);
client.provide("get /v1/user/retrieve", { id: "10" });
*/
"
Expand Down Expand Up @@ -333,7 +333,7 @@ export type Implementation = (
params: Record<string, any>,
) => Promise<any>;
export class ExpressZodAPIClient {
export class Client {
public constructor(protected readonly implementation: Implementation) {}
public provide<K extends Request>(
request: K,
Expand Down Expand Up @@ -366,7 +366,7 @@ export const exampleImplementation: Implementation = async (
const isJSON = contentType.startsWith("application/json");
return response[isJSON ? "json" : "text"]();
};
const client = new ExpressZodAPIClient(exampleImplementation);
const client = new Client(exampleImplementation);
client.provide("get /v1/user/retrieve", { id: "10" });
*/
"
Expand Down Expand Up @@ -875,7 +875,7 @@ export type Implementation = (
params: Record<string, any>,
) => Promise<any>;
export class ExpressZodAPIClient {
export class Client {
public constructor(protected readonly implementation: Implementation) {}
public provide<K extends Request>(
request: K,
Expand Down Expand Up @@ -908,7 +908,7 @@ export const exampleImplementation: Implementation = async (
const isJSON = contentType.startsWith("application/json");
return response[isJSON ? "json" : "text"]();
};
const client = new ExpressZodAPIClient(exampleImplementation);
const client = new Client(exampleImplementation);
client.provide("get /v1/user/retrieve", { id: "10" });
*/
"
Expand Down Expand Up @@ -1478,7 +1478,7 @@ export type Implementation = (
params: Record<string, any>,
) => Promise<any>;
export class ExpressZodAPIClient {
export class Client {
public constructor(protected readonly implementation: Implementation) {}
public provide<K extends Request>(
request: K,
Expand Down Expand Up @@ -1511,7 +1511,7 @@ export const exampleImplementation: Implementation = async (
const isJSON = contentType.startsWith("application/json");
return response[isJSON ? "json" : "text"]();
};
const client = new ExpressZodAPIClient(exampleImplementation);
const client = new Client(exampleImplementation);
client.provide("get /v1/user/retrieve", { id: "10" });
*/
"
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("Migration", () => {
`new Documentation();`,
`new EndpointsFactory(new ResultHandler());`,
`new EventStreamFactory({});`,
`new Client();`,
],
invalid: [
{
Expand Down Expand Up @@ -107,6 +108,20 @@ describe("Migration", () => {
},
],
},
{
code: `new ExpressZodAPIClient();`,
output: `new Client();`,
errors: [
{
messageId: "change",
data: {
subject: "class",
from: "ExpressZodAPIClient",
to: "Client",
},
},
],
},
],
});
});

0 comments on commit 88a0a66

Please sign in to comment.