Last active
November 5, 2024 18:13
-
-
Save alexandrebodin/96e739768a2866d6f5ebb7a9950c3e66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = { | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SDK internals | |
export const createSDK = <TSchema extends Schema>({ baseUrl, auth, defaults }, schema: TSchema): SDKInstance<TSchema> => { | |
const sdk = { | |
fetch() { | |
/* ... */ | |
} | |
queries: generateQueries(schema) | |
} | |
return sdk; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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