Skip to content

Instantly share code, notes, and snippets.

@alexandrebodin
Last active November 5, 2024 18:13
Show Gist options
  • Save alexandrebodin/96e739768a2866d6f5ebb7a9950c3e66 to your computer and use it in GitHub Desktop.
Save alexandrebodin/96e739768a2866d6f5ebb7a9950c3e66 to your computer and use it in GitHub Desktop.
// Generated file
// Option 1
import { createSDK as createBaseSDK, type Options } from '@strapi/sdk-js'
export const schema = // generated
export const createSDK = (options: Options) => {
return createBaseSDK(options, schema)
}
// Option 2 - the cli could simply generate a schema file
import type { Schema } from '@strapi/sdk-js';
export default schema: Schema = {
};
// SDK internals
export const createSDK = <TSchema extends Schema>({ baseUrl, auth, defaults }, schema: TSchema): SDKInstance<TSchema> => {
const sdk = {
fetch() {
/* ... */
}
queries: generateQueries(schema)
}
return sdk;
}
// User code
// Option 1 - create the sdk via the generated file
import { createSDK } from './strapi.ts
const sdk = createSDK(...);
sdk.queries
sdk.fetch
// Option 2 - use the package directly
import { createSDK } from '@strapi/sdk-js'
import schema from './strapi.ts'
const sdk = createSDK({
url: '...',
auth: {
token: '',
},
defaults: {},
schema // schema can be optional and in that case you just get a raw SDK with fetching capabilities or you can manually pass a schema
});
sdk.queries
sdk.fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment