Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new @prismicio/client's WriteClient #65

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"quick-lru": "^6.1.1"
},
"devDependencies": {
"@prismicio/client": "^7.6.0",
"@prismicio/client": "^7.10.1",
"@prismicio/mock": "^0.3.7",
"@size-limit/preset-small-lib": "^8.2.6",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand Down
24 changes: 24 additions & 0 deletions src/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,37 @@ export function generateTypes(config: GenerateTypesConfig = {}): string {
clientModuleCode = addSection(
`interface CreateClient {
(repositoryNameOrEndpoint: string, options?: ${clientImportName}.ClientConfig): ${clientImportName}.Client<AllDocumentTypes>;
}`,
clientModuleCode,
);
clientModuleCode = addSection(
`interface CreateWriteClient {
(repositoryNameOrEndpoint: string, options: ${clientImportName}.WriteClientConfig): ${clientImportName}.WriteClient<AllDocumentTypes>;
}`,
clientModuleCode,
);
clientModuleCode = addSection(
`interface CreateMigration {
(): ${clientImportName}.Migration<AllDocumentTypes>;
}`,
clientModuleCode,
);
} else {
clientModuleCode = addSection(
`interface CreateClient {
(repositoryNameOrEndpoint: string, options?: ${clientImportName}.ClientConfig): ${clientImportName}.Client;
}`,
clientModuleCode,
);
clientModuleCode = addSection(
`interface CreateWriteClient {
(repositoryNameOrEndpoint: string, options?: ${clientImportName}.WriteClientConfig): ${clientImportName}.WriteClient;
}`,
clientModuleCode,
);
clientModuleCode = addSection(
`interface CreateMigration {
(): ${clientImportName}.Migration;
}`,
clientModuleCode,
);
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/generateTypes.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2845,6 +2845,14 @@ declare module \\"@prismicio/client\\" {
(repositoryNameOrEndpoint: string, options?: prismicClient.ClientConfig): prismicClient.Client<AllDocumentTypes>;
}

interface CreateWriteClient {
(repositoryNameOrEndpoint: string, options: prismicClient.WriteClientConfig): prismicClient.WriteClient<AllDocumentTypes>;
}

interface CreateMigration {
(): prismicClient.Migration<AllDocumentTypes>;
}

namespace Content {
export type {
EgetDocument,
Expand Down
204 changes: 204 additions & 0 deletions test/generateTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,131 @@ it("includes untyped `@prismicio/client` in CreateClient interface if no Custom
);
});

it("includes @prismicio/client module declaration including a CreateWriteClient interface if configured", (ctx) => {
const res = lib.generateTypes({
clientIntegration: {
includeCreateClientInterface: true,
},
customTypeModels: [ctx.mock.model.customType({ id: "foo" })],
});

const file = parseSourceFile(res);

const importDeclaration =
file.getImportDeclarationOrThrow("@prismicio/client");

expect(importDeclaration.getNamespaceImportOrThrow().getText()).toBe(
"prismicClient",
);
expect(importDeclaration.isTypeOnly()).toBe(true);

const createWriteClientInterface = file
.getModuleOrThrow('"@prismicio/client"')
.getInterfaceOrThrow("CreateWriteClient");
const callSignatures = createWriteClientInterface.getCallSignatures();
const firstCallSignature = callSignatures[0];

expect(callSignatures.length).toBe(1);

expect(
firstCallSignature
.getParameterOrThrow("repositoryNameOrEndpoint")
.getTypeNodeOrThrow()
.getText(),
).toBe(`string`);

expect(
firstCallSignature
.getParameterOrThrow("options")
.getTypeNodeOrThrow()
.getText(),
).toBe(`prismicClient.WriteClientConfig`);
expect(firstCallSignature.getParameterOrThrow("options").isOptional()).toBe(
false,
);

expect(firstCallSignature.getReturnTypeNodeOrThrow().getText()).toBe(
"prismicClient.WriteClient<AllDocumentTypes>",
);

expect(() => {
file.getImportDeclarationOrThrow("@prismicio/client");
}).not.throws("imports `@prismicio/client`");
});

it("includes untyped `@prismicio/client` in CreateWriteClient interface if no Custom Type models are provided", () => {
const res = lib.generateTypes({
clientIntegration: {
includeCreateClientInterface: true,
},
customTypeModels: [],
});

const file = parseSourceFile(res);
const createWriteClientInterface = file
.getModuleOrThrow('"@prismicio/client"')
.getInterfaceOrThrow("CreateWriteClient");
const callSignatures = createWriteClientInterface.getCallSignatures();

expect(callSignatures[0].getReturnTypeNodeOrThrow().getText()).toBe(
"prismicClient.WriteClient",
);
});

it("includes @prismicio/client module declaration including a CreateMigration interface if configured", (ctx) => {
const res = lib.generateTypes({
clientIntegration: {
includeCreateClientInterface: true,
},
customTypeModels: [ctx.mock.model.customType({ id: "foo" })],
});

const file = parseSourceFile(res);

const importDeclaration =
file.getImportDeclarationOrThrow("@prismicio/client");

expect(importDeclaration.getNamespaceImportOrThrow().getText()).toBe(
"prismicClient",
);
expect(importDeclaration.isTypeOnly()).toBe(true);

const createMigrationInterface = file
.getModuleOrThrow('"@prismicio/client"')
.getInterfaceOrThrow("CreateMigration");
const callSignatures = createMigrationInterface.getCallSignatures();
const firstCallSignature = callSignatures[0];

expect(callSignatures.length).toBe(1);

expect(firstCallSignature.getReturnTypeNodeOrThrow().getText()).toBe(
"prismicClient.Migration<AllDocumentTypes>",
);

expect(() => {
file.getImportDeclarationOrThrow("@prismicio/client");
}).not.throws("imports `@prismicio/client`");
});

it("includes untyped `@prismicio/client` in CreateMigration interface if no Custom Type models are provided", () => {
const res = lib.generateTypes({
clientIntegration: {
includeCreateClientInterface: true,
},
customTypeModels: [],
});

const file = parseSourceFile(res);
const createMigrationInterface = file
.getModuleOrThrow('"@prismicio/client"')
.getInterfaceOrThrow("CreateMigration");
const callSignatures = createMigrationInterface.getCallSignatures();

expect(callSignatures[0].getReturnTypeNodeOrThrow().getText()).toBe(
"prismicClient.Migration",
);
});

it("includes @prismicio/client Content namespace containing all document and Slice types if configured", (ctx) => {
const res = lib.generateTypes({
clientIntegration: {
Expand Down Expand Up @@ -274,6 +399,85 @@ it("uses the existing prismic import if the `@prismicio/client` types provider i
}).not.throws("imports `@prismicio/client`");
});

it("uses the existing prismic import if the `@prismicio/client` types provider is used with the CreateWriteClient interface", (ctx) => {
const res = lib.generateTypes({
clientIntegration: {
includeCreateClientInterface: true,
},
typesProvider: "@prismicio/client",
customTypeModels: [ctx.mock.model.customType({ id: "foo" })],
});

const file = parseSourceFile(res);

const importDeclaration =
file.getImportDeclarationOrThrow("@prismicio/client");

expect(importDeclaration.getNamespaceImportOrThrow().getText()).toBe(
"prismic",
);
expect(importDeclaration.isTypeOnly()).toBe(true);

const createWriteClientInterface = file
.getModuleOrThrow('"@prismicio/client"')
.getInterfaceOrThrow("CreateWriteClient");
const callSignatures = createWriteClientInterface.getCallSignatures();
const firstCallSignature = callSignatures[0];

expect(callSignatures.length).toBe(1);

expect(
firstCallSignature
.getParameterOrThrow("options")
.getTypeNodeOrThrow()
.getText(),
).toBe(`prismic.WriteClientConfig`);

expect(firstCallSignature.getReturnTypeNodeOrThrow().getText()).toBe(
"prismic.WriteClient<AllDocumentTypes>",
);

expect(() => {
file.getImportDeclarationOrThrow("@prismicio/client");
}).not.throws("imports `@prismicio/client`");
});

it("uses the existing prismic import if the `@prismicio/client` types provider is used with the CreateMigration interface", (ctx) => {
const res = lib.generateTypes({
clientIntegration: {
includeCreateClientInterface: true,
},
typesProvider: "@prismicio/client",
customTypeModels: [ctx.mock.model.customType({ id: "foo" })],
});

const file = parseSourceFile(res);

const importDeclaration =
file.getImportDeclarationOrThrow("@prismicio/client");

expect(importDeclaration.getNamespaceImportOrThrow().getText()).toBe(
"prismic",
);
expect(importDeclaration.isTypeOnly()).toBe(true);

const createMigrationInterface = file
.getModuleOrThrow('"@prismicio/client"')
.getInterfaceOrThrow("CreateMigration");
const callSignatures = createMigrationInterface.getCallSignatures();
const firstCallSignature = callSignatures[0];

expect(callSignatures.length).toBe(1);

expect(firstCallSignature.getReturnTypeNodeOrThrow().getText()).toBe(
"prismic.Migration<AllDocumentTypes>",
);

expect(() => {
file.getImportDeclarationOrThrow("@prismicio/client");
}).not.throws("imports `@prismicio/client`");
});

it("outputs correct code style", (ctx) => {
const customTypeModels = Array.from({ length: 5 }, () =>
ctx.mock.model.customType({
Expand Down
Loading