A type-safe MicroCMS SDK Generator
npm install microcms_sdk_generatormicrocms_sdk_generator is a Deno tool designed to automatically generate
TypeScript SDKs from your microCMS API schema. The tool
leverages Zod schemas to ensure TypeScript type safety.
Installation and usage are easily handled via npm or Deno.
- Features
- Getting Started
- Installation with Deno
- Installation with npm
- Usage
- SDK Usage
- Schema Files
- Contributing
- License
- FAQ
- Generates TypeScript SDKs from your microCMS API
schema automatically.
- Utilizes Zod schemas to maintain TypeScript type safety.
- Supports usage in both server-side and client-side environments.
Execute the following command for installation using Deno.
``sh`
deno install --allow-read --allow-write https://deno.land/x/microcms_sdk_generator/microcms_sdk_generator.ts
> microcms\_sdk\_generator \| Deno
Or execute directly.
`sh`
deno run --allow-read --allow-write https://deno.land/x/microcms_sdk_generator/microcms_sdk_generator.ts
Execute the following command for installation using npm.
`sh`
npm install --global microcms_sdk_generator
> microcms\_sdk\_generator \- npm
Or execute directly.
`sh`
npx microcms_sdk_generator
To use the tool, specify the directory of the schema files and the destination
TypeScript file.
`sh`
microcms_sdk_generator
For example, execute the following command on the root of this repository.
`sh`
microcms_sdk_generator ./src/testdata/schemas ./src/testdata/generated.ts
Initialize the client with your service domain and API key.
`typescript`
const client = createClient({
serviceDomain: "YOUR_SERVICE_DOMAIN",
apiKey: "YOUR_API_KEY",
});
Use the client to make requests as follows.
`typescript${endpointName}
const resp = await client[].list({...})${endpointName}
const resp = await client[].get({...})${endpointName}
const resp = await client[].post({...})${endpointName}
const resp = await client[].put({...})${endpointName}
const resp = await client[].patch({...})${endpointName}
const resp = await client[].delete({...})${endpointName}
const resp = await client[].listMetadata({...})${endpointName}
const resp = await client[].getMetadata({...})${endpointName}
const resp = await client[].patchStatus({...})`
For example, for the sampleForListApi endpoint.
`typescript`
const listResp = await client.sampleForListApi.list({
limit: 2,
orders: ["-createdAt"],
});
if (!listResp.ok) {
throw listResp;
}
console.log(listResp.data.contents);
Schema files need to be placed under the schema directory in the following
structure:
- {schema directory}
- list
- {endpointName}.json
- ...
- object
- {endpointName}.json
- ...
NOTE: microCMS currently does not support exporting schemas per endpoint. You
will need to use the exported schema file, but this file does not resolve
endpointName and API type (list or object)`. Therefore, you should structure
your files as shown above.
We welcome bug reports and feature requests through GitHub issues. Pull requests
are also welcome.
This project is open-sourced under the MIT License. See the LICENSE
file for details.
You can prepare a fetcher capable of retrying, and specify it as a customFetcher
in option.
e.g.
vercel/async\-retry: Retrying made simple, easy and async