@starlightcms/js-sdk 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/client.js +1 -1
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/esm/client.js +1 -1
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/types/index.d.ts +1 -1
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/types/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cjs/client.js
CHANGED
|
@@ -62,7 +62,7 @@ const Collection_2 = __importDefault(require("./instances/Collection"));
|
|
|
62
62
|
*/
|
|
63
63
|
function makeClient(config = {}) {
|
|
64
64
|
var _a, _b, _c;
|
|
65
|
-
let baseUrl = (_a = config.baseUrl) !== null && _a !== void 0 ? _a : 'https://query.
|
|
65
|
+
let baseUrl = (_a = config.baseUrl) !== null && _a !== void 0 ? _a : 'https://query.starlightcms.io/v2';
|
|
66
66
|
let workspace = (_b = config.workspace) !== null && _b !== void 0 ? _b : '';
|
|
67
67
|
let debug = (_c = config.debug) !== null && _c !== void 0 ? _c : false;
|
|
68
68
|
const client = {
|
package/dist/cjs/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,qCAAyC;AACzC,8DAAiD;AACjD,8DAAiD;AACjD,sEAAyD;AACzD,wEAA2D;AAC3D,8DAAiD;AACjD,gEAAmD;AACnD,wEAA2D;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,UAAU,CAGxB,SAA0B,EAAE;;IAE5B,IAAI,OAAO,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI
|
|
1
|
+
{"version":3,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sB,EAAC,IAAI,CAAC,CAAA;QACrC,CAAC;QAED,UAAU,CAAC,IAAqB;YAC9B,OAAO,IAAA,oBAAsB,EAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QAC3C,CAAC;QAED,IAAI,KAAK;YACP,OAAO,IAAA,eAAiB,EAAC,IAAI,CAAC,CAAA;QAChC,CAAC;QAED,IAAI,MAAM;YACR,OAAO,IAAA,gBAAkB,EAAC,IAAI,CAAC,CAAA;QACjC,CAAC;KACF,CAAA;IAED,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,EAAE,IAAI;YACd,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE;gBAC1D,OAAO,IAAA,eAAiB,EAAC,MAAM,EAAE,IAAI,CAAC,CAAA;aACvC;YAED,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QAClC,CAAC;KACF,CAAyD,CAAA;AAC5D,CAAC;AA5FD,gCA4FC","sourcesContent":["import {\n DefaultModelDefinition,\n DynamicStarlightClient,\n StarlightClient,\n StarlightConfig,\n WorkspaceModelDefinition,\n} from './types'\nimport { StarlightError } from './errors'\nimport makeModelSelector from './selectors/Model'\nimport makeModelInstance from './instances/Model'\nimport makeSingletonSelector from './selectors/Singleton'\nimport makeCollectionSelector from './selectors/Collection'\nimport makeMediaSelector from './selectors/Media'\nimport makeSearchSelector from './selectors/Search'\nimport makeCollectionInstance from './instances/Collection'\n\n/**\n * Returns a new {@link DynamicStarlightClient}, which is a\n * {@link StarlightClient} with support to create\n * {@apilink ModelInstance | ModelInstances} using the dynamic syntax. To learn\n * which methods a client supports, see {@link StarlightClient}.\n *\n * This function accepts a {@link StarlightConfig} object and can be used to\n * create new clients that connect to a single Starlight workspace. Each client\n * returned by this function is separate and independent of the others.\n *\n * If you only need to make requests to a single workspace, it's probably easier\n * to use the SDK's default client. To use it, you only need to\n * import the default object exported by the SDK:\n *\n * ```ts\n * // `Starlight` below is the default StarlightClient.\n * import Starlight from '@starlightcms/js-sdk'\n * ```\n *\n * See {@link default} for more info on how to use the default client.\n *\n * @example Creating a new client and exporting it.\n * ```ts\n * import { makeStarlightClient } from '@starlightcms/js-sdk'\n *\n * const BlogClient = makeStarlightClient({\n * workspace: '123123123',\n * debug: true\n * })\n *\n * // Feel free to export the new client so your application can use it.\n * export default BlogClient\n * ```\n *\n * @param config The client configuration object. You need to provide at least\n * the `workspace` property. See {@link StarlightConfig} to view all the\n * available options.\n * @group Client\n */\nexport function makeClient<\n D extends WorkspaceModelDefinition = DefaultModelDefinition\n>(\n config: StarlightConfig = {}\n): DynamicStarlightClient<D & WorkspaceModelDefinition> {\n let baseUrl = config.baseUrl ?? 'https://query.starlightcms.io/v2'\n let workspace = config.workspace ?? ''\n let debug = config.debug ?? false\n\n const client: StarlightClient = {\n configure(config) {\n baseUrl = config.baseUrl ?? baseUrl\n workspace = config.workspace ?? workspace\n debug = config.debug ?? debug\n },\n\n log(message, ...optionalParams) {\n if (debug) console.log(message, ...optionalParams)\n },\n\n getBaseUrl() {\n if (!workspace.length) {\n throw new Error(\n 'No workspace defined. Use the `configure()` method on the Starlight client to define one.'\n )\n }\n\n return `${baseUrl}/workspaces/${workspace}`\n },\n\n async get(path, params = {}, options) {\n const filteredParams = Object.keys(params).reduce((accumulator, key) => {\n const param = params[key]\n\n if (param || param === false) accumulator[key] = String(param)\n\n return accumulator\n }, {} as Record<string, string>)\n\n const searchParams = new URLSearchParams(filteredParams).toString()\n const finalPath = searchParams ? `${path}?${searchParams}` : path\n\n this.log(`Starlight - GET ${finalPath}`)\n\n const response = await fetch(`${this.getBaseUrl()}${finalPath}`, options)\n\n if (response.status >= 200 && response.status < 300) {\n return await response.json()\n } else {\n const message = `Starlight - GET ${path} returned ${response.status}: ${response.statusText}`\n throw new StarlightError(message, response)\n }\n },\n\n get models() {\n return makeModelSelector(this)\n },\n\n model(slug) {\n return makeModelInstance(this, slug as string)\n },\n\n get singletons() {\n return makeSingletonSelector(this)\n },\n\n get collections() {\n return makeCollectionSelector(this)\n },\n\n collection(slug: string | number) {\n return makeCollectionInstance(this, slug)\n },\n\n get media() {\n return makeMediaSelector(this)\n },\n\n get search() {\n return makeSearchSelector(this)\n },\n }\n\n return new Proxy(client, {\n get(target, prop) {\n if (typeof prop === 'string' && !Reflect.has(target, prop)) {\n return makeModelInstance(target, prop)\n }\n\n return Reflect.get(target, prop)\n },\n }) as DynamicStarlightClient<D & WorkspaceModelDefinition>\n}\n"]}
|
|
@@ -16,7 +16,7 @@ export * from './selectors';
|
|
|
16
16
|
* provides a default one which can be used by IDEs to provide auto-completion.
|
|
17
17
|
* The default URL points to version 2 of the Query API.
|
|
18
18
|
*/
|
|
19
|
-
export declare type BaseUrl = 'https://query.
|
|
19
|
+
export declare type BaseUrl = 'https://query.starlightcms.io/v2' | string;
|
|
20
20
|
/**
|
|
21
21
|
* The available options to configure a {@link StarlightClient}.
|
|
22
22
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,c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IACpD;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AASA,2CAAwB;AACxB,6CAA0B;AAC1B,2CAAwB;AACxB,8CAA2B;AAC3B,8CAA2B","sourcesContent":["import { CollectionEntityTypes, SerializedData } from './entities'\nimport { DynamicModelSelector } from '../selectors/Model'\nimport { DynamicModelInstance } from '../instances/Model'\nimport { SingletonSelector } from '../selectors/Singleton'\nimport { DynamicCollectionSelector } from '../selectors/Collection'\nimport { MediaSelector } from '../selectors/Media'\nimport { SearchSelector } from '../selectors/Search'\nimport { CollectionInstance } from '../instances/Collection'\n\nexport * from './fields'\nexport * from './entities'\nexport * from './visual'\nexport * from './instances'\nexport * from './selectors'\n\n/**\n * This is a utility type that allows any string to be used as a URL, but\n * provides a default one which can be used by IDEs to provide auto-completion.\n * The default URL points to version 2 of the Query API.\n */\nexport type BaseUrl = 'https://query.starlight.sh/v2' | string\n\n/**\n * The available options to configure a {@link StarlightClient}.\n *\n * `workspace` is required when creating new clients or configuring the\n * {@apilink default | default client}.\n * @group Client\n */\nexport type StarlightConfig = {\n /**\n * The ID of the workspace that the client should use to request data.\n *\n * Note: the current APIs only support using workspace IDs as identifiers,\n * and **not** slugs. Slug support will be added in the future. You can find\n * the workspace ID in the left-side menu on the Starlight interface or below\n * the workspace name in the workspace list.\n */\n workspace?: string\n /**\n * The Starlight Query API URL, including the version, and without a trailing\n * slash. Defaults to the production Query API URL.\n *\n * You only need to set this if you're not using Starlight's production APIs.\n */\n baseUrl?: BaseUrl\n /**\n * When true, logs all API requests in the console. Defaults to false.\n */\n debug?: boolean\n}\n\n/**\n * The Starlight client is the main entry point for any requests you might want\n * to make to Starlight's APIs.\n *\n * There's two ways of interacting with a client: using the\n * {@apilink default | default client} exported by the SDK or creating a new\n * client instance using the\n * {@apilink makeStarlightClient | makeStarlightClient function}. Follow the\n * provided links to learn how to use each method.\n *\n * Either way you choose to interact, all clients expose the same methods and\n * accessors that provide ways to request data. These methods and accessors\n * return Selector or Instance objects, which are documented in the sections\n * of the same name found in this API's sidebar.\n *\n * @group Client\n */\nexport interface StarlightClient<\n D extends WorkspaceModelDefinition = DefaultModelDefinition\n> {\n /**\n * Updates the client configuration. See {@link StarlightConfig} to see all\n * the available options.\n *\n * If you want to use the {@apilink default | default SDK client}, you need to\n * set its workspace using this method in your application. You should run\n * this method only once, and as soon as possible in your application\n * lifecycle. For instance, when using React:\n *\n * ```js\n * // src/index.js\n * import { createRoot } from \"react-dom/client\"\n * import Starlight from '@starlightcms/js-sdk'\n * import MyApp from './MyApp.js'\n *\n * // We only need to run this once. In this case, we're\n * // running it before initializing our React app.\n * Starlight.configure({\n * workspace: '1234567890'\n * })\n *\n * const rootElement = document.getElementById(\"root\")\n * const root = createRoot(rootElement)\n *\n * root.render(<MyApp />)\n * ```\n *\n * @param config A configuration object. See {@link StarlightConfig} to view\n * all available options.\n * @category Other Methods\n */\n configure(config: StarlightConfig): void\n\n /**\n * Logs a message (or any kind of data, really) into the console if the debug\n * configuration is true. Uses `console.log` internally.\n *\n * This function's type definition is the same as the `console.log` function.\n *\n * @param message The data that will be logged.\n * @param optionalParams Additional data to be logged or string substitutions.\n * @see [MDN documentation on console.log](https://developer.mozilla.org/en-US/docs/web/api/console/log)\n * @internal\n */\n log(message?: unknown, ...optionalParams: unknown[]): void\n\n /**\n * Returns the base API URL for requests, including the configured workspace.\n * Doesn't include a trailing slash.\n *\n * @internal\n */\n getBaseUrl(): string\n\n /**\n * Returns a Promise that results in a response or throws a StarlightError.\n * The response will be the parsed JSON data sent by the API.\n *\n * This method is used internally by all Selectors and Instances to request\n * data to Starlight's Query API, but it can also be used standalone if\n * desired.\n *\n * This method automatically prefixes the given path with the API URL, but it\n * doesn't include a trailing slash, so be sure to include one at the start\n * of your path.\n *\n * @param path The path to GET. Will be prefixed with the API URL. Must start\n * with a forward slash (/).\n * @param params An optional object of values that will be converted to a\n * string and passed as GET params.\n * @param options An optional configuration object passed to the fetch method.\n * Check [MDN documentation on fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch)\n * to see the available options.\n * @typeParam T - The shape of the expected response on success. Client\n * methods generally pass {@link StarlightItemResponse} or\n * {@link StarlightListResponse} types here.\n * @throws {@link StarlightError} on any errors.\n * @category Other Methods\n */\n get<T = Record<string, unknown>>(\n path: string,\n params?: Record<string, string | number | boolean | undefined>,\n options?: RequestInit\n ): Promise<T>\n\n /**\n * Returns a {@link DynamicModelSelector}, which is a {@link ModelSelector}\n * with support for creating {@apilink ModelInstance | ModelInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.models.list()\n * ```\n *\n * See {@link DynamicModelSelector} for more info.\n *\n * @category Selector Accessors\n */\n get models(): DynamicModelSelector<D>\n\n /**\n * Returns a {@link DynamicModelInstance}, which is a\n * {@link ModelInstance} with support for creating\n * {@apilink ModelCategoryInstance | ModelCategoryInstances} using the dynamic syntax. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.model('posts').entries.list()\n * ```\n *\n * See {@link DynamicModelInstance} for more info.\n *\n * @category Instance Methods\n */\n model<S extends keyof D>(slug: S): DynamicModelInstance<D[S]>\n\n /**\n * Returns a {@link SingletonSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.singletons.list()\n * ```\n *\n * See {@link SingletonSelector} for more info.\n *\n * @category Selector Accessors\n */\n get singletons(): SingletonSelector\n\n /**\n * Returns a {@link DynamicCollectionSelector}, which is a\n * {@link CollectionSelector} with support for creating\n * {@apilink CollectionInstance | CollectionInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collections.list()\n * ```\n *\n * See {@link DynamicCollectionSelector} for more info.\n *\n * @category Selector Accessors\n */\n get collections(): DynamicCollectionSelector\n\n /**\n * Returns a {@link CollectionInstance}. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection('featured-posts').items()\n * ```\n *\n * See {@link CollectionInstance} for more info.\n *\n * @example Typing the returned CollectionInstance.\n * ```ts\n * import Starlight, { MediaObject } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection<MediaObject>('carousel-images').items()\n * ```\n *\n * @typeParam T - The type of entity this Collection holds. You can pass this\n * type parameter to correctly type the response of the\n * {@apilink CollectionInstance.items} method.\n * @category Instance Methods\n */\n collection<T extends CollectionEntityTypes>(\n slug: string | number\n ): CollectionInstance<T>\n\n /**\n * Returns a {@link MediaSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.media.list()\n * ```\n *\n * See {@link MediaSelector} for more info.\n *\n * @category Selector Accessors\n */\n get media(): MediaSelector\n\n /**\n * Returns a {@link SearchSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.search.entries()\n * ```\n *\n * See {@link SearchSelector} for more info.\n *\n * @category Selector Accessors\n */\n get search(): SearchSelector\n}\n\n/**\n * This type adds support for the dynamic syntax to the StarlightClient\n * interface, which allows users to create\n * {@apilink DynamicModelInstance| DynamicModelInstances} dynamically.\n * See {@link StarlightClient} to learn which methods it provides. Also see\n * {@doclink requests-and-responses#dynamic-syntax | Dynamic Instances}\n * documentation to learn more about the dynamic syntax.\n *\n *\n * This allows TypeScript to correctly type all models defined by the user\n * in the DefaultModelDefinition type, aside from letting the user using any\n * \"unknown\" model slug, which is provided by the WorkspaceModelDefinition type.\n *\n * This type is only aware of the DefaultModelDefinition type because the\n * StarlightClient uses it by default when no model definition type is passed.\n *\n * @group Client\n */\nexport type DynamicStarlightClient<T extends WorkspaceModelDefinition> =\n StarlightClient<T> & {\n [K in keyof T]: DynamicModelInstance<T[K]>\n }\n\n/**\n * This interface represents an API response that returns a single entity, like\n * a single {@link Entry} or a single {@link MediaObject}, for instance.\n *\n * It contains only one field: `data`. This field type is generic and\n * depends on the kind of request that was made to the API.\n *\n * All SDK request methods that returna single entity\n * will return this interface.\n *\n * @group Client\n */\nexport interface StarlightItemResponse<T> {\n /**\n * The entity returned by the API request. Its type depends on which request\n * was made. SDK methods will generally type this parameter automatically.\n */\n data: T\n}\n\n/**\n * This interface represents an API response that returns a list of entities,\n * like a list of {@apilink Entry | Entries} or a list of items from a\n * {@link Collection}.\n *\n * It contains a `data` parameter, which is an array of items with a generic\n * type that depends on the kind of request that was made, and two metadata\n * objects: `links` and `meta`. Metadata is useful for pagination.\n *\n * All SDK request methods that return a list of entities will return this\n * interface.\n *\n * @group Client\n */\nexport interface StarlightListResponse<T> {\n /**\n * The list of entities returned by the API request. Its type depends on which\n * request was made. SDK methods will generally type\n * this parameter automatically.\n */\n data: T[]\n /**\n * An object containing useful links for easier pagination. All links points\n * to the same list requested, but with a varying `page` parameter.\n */\n links: {\n /**\n * A link for the first page of the list.\n */\n first: string\n /**\n * A link for the last page of the list.\n */\n last: string\n /**\n * A link to the previous page of the list, if there's any.\n */\n prev?: string\n /**\n * A link to the next page of the list, if there's any.\n */\n next?: string\n }\n /**\n * An object with useful metadata related to the current list. It can be used\n * in applications to create pagination logic and interfaces.\n *\n * `from` and `to` are indexes indicating which items start and finish the\n * current page of the list. For instance, in a list with 100 items with 15\n * items per page, on the first page, `from` and `to` will be `1` and `15`\n * respectively.\n */\n meta: {\n /**\n * The number of the current page.\n */\n current_page: number\n /**\n * The number of the last page for the current list.\n */\n last_page: number\n /**\n * The index of the first item on this page out of all list items.\n */\n from: number\n /**\n * The index of the last item on this page out of all list items.\n */\n to: number\n /**\n * The number of items per page.\n */\n per_page: number\n /**\n * The total number of items in the current list.\n */\n total: number\n }\n}\n\n/**\n * The DefaultModelDefinition type is used to correctly type Entry data when\n * requesting it using a StarlightClient.\n *\n * Without using a DefaultModelDefinition, Entry data will be typed as a\n * \"generic\" JS object which can hold any kind of data:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry> on request success.\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<Record<string, unknown>>. This means that\n * // `data` can hold anything, which is not type-safe.\n * const helloWorld = response.data\n * ```\n *\n * One way of safely adding type information to Entry objects is by passing a\n * data definition type to the EntrySelector when requesting something:\n *\n * ```ts\n * import Starlight, { VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * // response type will now be StarlightItemResponse<Entry<PostsFields>>.\n * const response = await Starlight.posts.entries<PostFields>.get('hello-world')\n *\n * // Now, helloWorld type is Entry<PostsFields>.\n * const helloWorld = response.data\n *\n * // Which means we can safely access its data, and IDEs will\n * // auto-complete the data field name below:\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * Note that we used \"Fields\" to define our data shape. We recommend using these\n * types, which are exported by this SDK, because they map to the field types\n * available in the Starlight admin when creating models, and should simplify\n * the type definition process. All available Field types are documented in the\n * {@apilink BooleanField | Data Fields section of this API}.\n *\n * However, passing model definition types around your application is not ideal,\n * and actually unnecessary. Using the DefaultModelDefinition type, all model\n * definitions can be automatically inferred.\n *\n * To get started, you need to create a\n * [type declaration file](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)\n * in which you'll define all your types. You can name it anything and place it\n * anywhere inside your project, but we recommend naming it `starlight.d.ts` and\n * placing it in the root folder of your source code, so it can be easily\n * imported later in your application if you ever need to reference these types.\n *\n * In this file, you can place all your model type definitions (including type\n * definitions for Singleton data, if you want!). Finally, you just need to\n * add a \"module definition block\" that will tell the SDK which types you expect\n * to receive when requesting models.\n *\n * Here's a complete exemple defining two models, Posts and Magazines:\n *\n * ```ts\n * import { StringField, VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * type MagazineFields = {\n * cover_image: MediaField\n * content: VisualField\n * issue_number: StringField\n * issue_year: StringField\n * }\n *\n * declare module '@starlightcms/js-sdk' {\n * export interface DefaultModelDefinition {\n * // Notice that each key in this interface is a Model slug!\n * posts: PostFields\n * magazines: MagazineFields\n * }\n * }\n * ```\n *\n * After creating this file, types should be automatically inferred without the\n * need to pass these types around:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry<PostsFields>>,\n * // which was implicitly inferred by the SDK!\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<PostsFields>\n * const helloWorld = response.data\n *\n * // Auto-complete will work just as before when we explicitly type the Entry!\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * @group Client\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface DefaultModelDefinition extends WorkspaceModelDefinition {}\n\n/**\n * This type is only used as a base for the DefaultModelDefinition type.\n *\n * @internal\n */\nexport interface WorkspaceModelDefinition {\n [slug: string]: SerializedData\n}\n\n/**\n * Request parameters used by most SDK `list()` methods.\n *\n * @group Request Parameters\n */\nexport interface BaseRequestParameters {\n [key: string]: string | number | boolean | undefined\n /**\n * The page requested.\n */\n page?: number\n /**\n * The limit of items per page.\n */\n limit?: number\n /**\n * A search query string.\n *\n * For instance, searching for \"out\" will match both \"check out!\" and\n * \"about us\". Search is not case-sensitive.\n */\n query?: string\n}\n\n/**\n * Request parameters used by most SDK methods that support advanced querying.\n *\n * @group Request Parameters\n */\nexport interface QueryableRequestParameters {\n /**\n * A search query string that matches a specific word within boundaries.\n * Search is not case-sensitive.\n *\n * For instance, searching for \"phone\" will match \"This is my phone!\"\n * but won't match \"This is my telephone!\".\n */\n 'query:word'?: string\n /**\n * A comma-separated list of fields to look up on when searching using\n * `query` or `query:word`. If undefined, all text fields will be searched on,\n * including Visual Editor fields. Search is not case-sensitive.\n *\n * For instance, to limit search on the \"content\" and \"summary\" fields of a\n * model, pass `'content,summary'`.\n */\n fields?: string\n /**\n * If defined, removes the given item from the list. Useful to create \"related\n * posts\" lists.\n *\n * Note: this field only accepts IDs, and not slugs. Only one ID is allowed.\n */\n except?: number\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AASA,2CAAwB;AACxB,6CAA0B;AAC1B,2CAAwB;AACxB,8CAA2B;AAC3B,8CAA2B","sourcesContent":["import { CollectionEntityTypes, SerializedData } from './entities'\nimport { DynamicModelSelector } from '../selectors/Model'\nimport { DynamicModelInstance } from '../instances/Model'\nimport { SingletonSelector } from '../selectors/Singleton'\nimport { DynamicCollectionSelector } from '../selectors/Collection'\nimport { MediaSelector } from '../selectors/Media'\nimport { SearchSelector } from '../selectors/Search'\nimport { CollectionInstance } from '../instances/Collection'\n\nexport * from './fields'\nexport * from './entities'\nexport * from './visual'\nexport * from './instances'\nexport * from './selectors'\n\n/**\n * This is a utility type that allows any string to be used as a URL, but\n * provides a default one which can be used by IDEs to provide auto-completion.\n * The default URL points to version 2 of the Query API.\n */\nexport type BaseUrl = 'https://query.starlightcms.io/v2' | string\n\n/**\n * The available options to configure a {@link StarlightClient}.\n *\n * `workspace` is required when creating new clients or configuring the\n * {@apilink default | default client}.\n * @group Client\n */\nexport type StarlightConfig = {\n /**\n * The ID of the workspace that the client should use to request data.\n *\n * Note: the current APIs only support using workspace IDs as identifiers,\n * and **not** slugs. Slug support will be added in the future. You can find\n * the workspace ID in the left-side menu on the Starlight interface or below\n * the workspace name in the workspace list.\n */\n workspace?: string\n /**\n * The Starlight Query API URL, including the version, and without a trailing\n * slash. Defaults to the production Query API URL.\n *\n * You only need to set this if you're not using Starlight's production APIs.\n */\n baseUrl?: BaseUrl\n /**\n * When true, logs all API requests in the console. Defaults to false.\n */\n debug?: boolean\n}\n\n/**\n * The Starlight client is the main entry point for any requests you might want\n * to make to Starlight's APIs.\n *\n * There's two ways of interacting with a client: using the\n * {@apilink default | default client} exported by the SDK or creating a new\n * client instance using the\n * {@apilink makeStarlightClient | makeStarlightClient function}. Follow the\n * provided links to learn how to use each method.\n *\n * Either way you choose to interact, all clients expose the same methods and\n * accessors that provide ways to request data. These methods and accessors\n * return Selector or Instance objects, which are documented in the sections\n * of the same name found in this API's sidebar.\n *\n * @group Client\n */\nexport interface StarlightClient<\n D extends WorkspaceModelDefinition = DefaultModelDefinition\n> {\n /**\n * Updates the client configuration. See {@link StarlightConfig} to see all\n * the available options.\n *\n * If you want to use the {@apilink default | default SDK client}, you need to\n * set its workspace using this method in your application. You should run\n * this method only once, and as soon as possible in your application\n * lifecycle. For instance, when using React:\n *\n * ```js\n * // src/index.js\n * import { createRoot } from \"react-dom/client\"\n * import Starlight from '@starlightcms/js-sdk'\n * import MyApp from './MyApp.js'\n *\n * // We only need to run this once. In this case, we're\n * // running it before initializing our React app.\n * Starlight.configure({\n * workspace: '1234567890'\n * })\n *\n * const rootElement = document.getElementById(\"root\")\n * const root = createRoot(rootElement)\n *\n * root.render(<MyApp />)\n * ```\n *\n * @param config A configuration object. See {@link StarlightConfig} to view\n * all available options.\n * @category Other Methods\n */\n configure(config: StarlightConfig): void\n\n /**\n * Logs a message (or any kind of data, really) into the console if the debug\n * configuration is true. Uses `console.log` internally.\n *\n * This function's type definition is the same as the `console.log` function.\n *\n * @param message The data that will be logged.\n * @param optionalParams Additional data to be logged or string substitutions.\n * @see [MDN documentation on console.log](https://developer.mozilla.org/en-US/docs/web/api/console/log)\n * @internal\n */\n log(message?: unknown, ...optionalParams: unknown[]): void\n\n /**\n * Returns the base API URL for requests, including the configured workspace.\n * Doesn't include a trailing slash.\n *\n * @internal\n */\n getBaseUrl(): string\n\n /**\n * Returns a Promise that results in a response or throws a StarlightError.\n * The response will be the parsed JSON data sent by the API.\n *\n * This method is used internally by all Selectors and Instances to request\n * data to Starlight's Query API, but it can also be used standalone if\n * desired.\n *\n * This method automatically prefixes the given path with the API URL, but it\n * doesn't include a trailing slash, so be sure to include one at the start\n * of your path.\n *\n * @param path The path to GET. Will be prefixed with the API URL. Must start\n * with a forward slash (/).\n * @param params An optional object of values that will be converted to a\n * string and passed as GET params.\n * @param options An optional configuration object passed to the fetch method.\n * Check [MDN documentation on fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch)\n * to see the available options.\n * @typeParam T - The shape of the expected response on success. Client\n * methods generally pass {@link StarlightItemResponse} or\n * {@link StarlightListResponse} types here.\n * @throws {@link StarlightError} on any errors.\n * @category Other Methods\n */\n get<T = Record<string, unknown>>(\n path: string,\n params?: Record<string, string | number | boolean | undefined>,\n options?: RequestInit\n ): Promise<T>\n\n /**\n * Returns a {@link DynamicModelSelector}, which is a {@link ModelSelector}\n * with support for creating {@apilink ModelInstance | ModelInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.models.list()\n * ```\n *\n * See {@link DynamicModelSelector} for more info.\n *\n * @category Selector Accessors\n */\n get models(): DynamicModelSelector<D>\n\n /**\n * Returns a {@link DynamicModelInstance}, which is a\n * {@link ModelInstance} with support for creating\n * {@apilink ModelCategoryInstance | ModelCategoryInstances} using the dynamic syntax. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.model('posts').entries.list()\n * ```\n *\n * See {@link DynamicModelInstance} for more info.\n *\n * @category Instance Methods\n */\n model<S extends keyof D>(slug: S): DynamicModelInstance<D[S]>\n\n /**\n * Returns a {@link SingletonSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.singletons.list()\n * ```\n *\n * See {@link SingletonSelector} for more info.\n *\n * @category Selector Accessors\n */\n get singletons(): SingletonSelector\n\n /**\n * Returns a {@link DynamicCollectionSelector}, which is a\n * {@link CollectionSelector} with support for creating\n * {@apilink CollectionInstance | CollectionInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collections.list()\n * ```\n *\n * See {@link DynamicCollectionSelector} for more info.\n *\n * @category Selector Accessors\n */\n get collections(): DynamicCollectionSelector\n\n /**\n * Returns a {@link CollectionInstance}. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection('featured-posts').items()\n * ```\n *\n * See {@link CollectionInstance} for more info.\n *\n * @example Typing the returned CollectionInstance.\n * ```ts\n * import Starlight, { MediaObject } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection<MediaObject>('carousel-images').items()\n * ```\n *\n * @typeParam T - The type of entity this Collection holds. You can pass this\n * type parameter to correctly type the response of the\n * {@apilink CollectionInstance.items} method.\n * @category Instance Methods\n */\n collection<T extends CollectionEntityTypes>(\n slug: string | number\n ): CollectionInstance<T>\n\n /**\n * Returns a {@link MediaSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.media.list()\n * ```\n *\n * See {@link MediaSelector} for more info.\n *\n * @category Selector Accessors\n */\n get media(): MediaSelector\n\n /**\n * Returns a {@link SearchSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.search.entries()\n * ```\n *\n * See {@link SearchSelector} for more info.\n *\n * @category Selector Accessors\n */\n get search(): SearchSelector\n}\n\n/**\n * This type adds support for the dynamic syntax to the StarlightClient\n * interface, which allows users to create\n * {@apilink DynamicModelInstance| DynamicModelInstances} dynamically.\n * See {@link StarlightClient} to learn which methods it provides. Also see\n * {@doclink requests-and-responses#dynamic-syntax | Dynamic Instances}\n * documentation to learn more about the dynamic syntax.\n *\n *\n * This allows TypeScript to correctly type all models defined by the user\n * in the DefaultModelDefinition type, aside from letting the user using any\n * \"unknown\" model slug, which is provided by the WorkspaceModelDefinition type.\n *\n * This type is only aware of the DefaultModelDefinition type because the\n * StarlightClient uses it by default when no model definition type is passed.\n *\n * @group Client\n */\nexport type DynamicStarlightClient<T extends WorkspaceModelDefinition> =\n StarlightClient<T> & {\n [K in keyof T]: DynamicModelInstance<T[K]>\n }\n\n/**\n * This interface represents an API response that returns a single entity, like\n * a single {@link Entry} or a single {@link MediaObject}, for instance.\n *\n * It contains only one field: `data`. This field type is generic and\n * depends on the kind of request that was made to the API.\n *\n * All SDK request methods that returna single entity\n * will return this interface.\n *\n * @group Client\n */\nexport interface StarlightItemResponse<T> {\n /**\n * The entity returned by the API request. Its type depends on which request\n * was made. SDK methods will generally type this parameter automatically.\n */\n data: T\n}\n\n/**\n * This interface represents an API response that returns a list of entities,\n * like a list of {@apilink Entry | Entries} or a list of items from a\n * {@link Collection}.\n *\n * It contains a `data` parameter, which is an array of items with a generic\n * type that depends on the kind of request that was made, and two metadata\n * objects: `links` and `meta`. Metadata is useful for pagination.\n *\n * All SDK request methods that return a list of entities will return this\n * interface.\n *\n * @group Client\n */\nexport interface StarlightListResponse<T> {\n /**\n * The list of entities returned by the API request. Its type depends on which\n * request was made. SDK methods will generally type\n * this parameter automatically.\n */\n data: T[]\n /**\n * An object containing useful links for easier pagination. All links points\n * to the same list requested, but with a varying `page` parameter.\n */\n links: {\n /**\n * A link for the first page of the list.\n */\n first: string\n /**\n * A link for the last page of the list.\n */\n last: string\n /**\n * A link to the previous page of the list, if there's any.\n */\n prev?: string\n /**\n * A link to the next page of the list, if there's any.\n */\n next?: string\n }\n /**\n * An object with useful metadata related to the current list. It can be used\n * in applications to create pagination logic and interfaces.\n *\n * `from` and `to` are indexes indicating which items start and finish the\n * current page of the list. For instance, in a list with 100 items with 15\n * items per page, on the first page, `from` and `to` will be `1` and `15`\n * respectively.\n */\n meta: {\n /**\n * The number of the current page.\n */\n current_page: number\n /**\n * The number of the last page for the current list.\n */\n last_page: number\n /**\n * The index of the first item on this page out of all list items.\n */\n from: number\n /**\n * The index of the last item on this page out of all list items.\n */\n to: number\n /**\n * The number of items per page.\n */\n per_page: number\n /**\n * The total number of items in the current list.\n */\n total: number\n }\n}\n\n/**\n * The DefaultModelDefinition type is used to correctly type Entry data when\n * requesting it using a StarlightClient.\n *\n * Without using a DefaultModelDefinition, Entry data will be typed as a\n * \"generic\" JS object which can hold any kind of data:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry> on request success.\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<Record<string, unknown>>. This means that\n * // `data` can hold anything, which is not type-safe.\n * const helloWorld = response.data\n * ```\n *\n * One way of safely adding type information to Entry objects is by passing a\n * data definition type to the EntrySelector when requesting something:\n *\n * ```ts\n * import Starlight, { VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * // response type will now be StarlightItemResponse<Entry<PostsFields>>.\n * const response = await Starlight.posts.entries<PostFields>.get('hello-world')\n *\n * // Now, helloWorld type is Entry<PostsFields>.\n * const helloWorld = response.data\n *\n * // Which means we can safely access its data, and IDEs will\n * // auto-complete the data field name below:\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * Note that we used \"Fields\" to define our data shape. We recommend using these\n * types, which are exported by this SDK, because they map to the field types\n * available in the Starlight admin when creating models, and should simplify\n * the type definition process. All available Field types are documented in the\n * {@apilink BooleanField | Data Fields section of this API}.\n *\n * However, passing model definition types around your application is not ideal,\n * and actually unnecessary. Using the DefaultModelDefinition type, all model\n * definitions can be automatically inferred.\n *\n * To get started, you need to create a\n * [type declaration file](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)\n * in which you'll define all your types. You can name it anything and place it\n * anywhere inside your project, but we recommend naming it `starlight.d.ts` and\n * placing it in the root folder of your source code, so it can be easily\n * imported later in your application if you ever need to reference these types.\n *\n * In this file, you can place all your model type definitions (including type\n * definitions for Singleton data, if you want!). Finally, you just need to\n * add a \"module definition block\" that will tell the SDK which types you expect\n * to receive when requesting models.\n *\n * Here's a complete exemple defining two models, Posts and Magazines:\n *\n * ```ts\n * import { StringField, VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * type MagazineFields = {\n * cover_image: MediaField\n * content: VisualField\n * issue_number: StringField\n * issue_year: StringField\n * }\n *\n * declare module '@starlightcms/js-sdk' {\n * export interface DefaultModelDefinition {\n * // Notice that each key in this interface is a Model slug!\n * posts: PostFields\n * magazines: MagazineFields\n * }\n * }\n * ```\n *\n * After creating this file, types should be automatically inferred without the\n * need to pass these types around:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry<PostsFields>>,\n * // which was implicitly inferred by the SDK!\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<PostsFields>\n * const helloWorld = response.data\n *\n * // Auto-complete will work just as before when we explicitly type the Entry!\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * @group Client\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface DefaultModelDefinition extends WorkspaceModelDefinition {}\n\n/**\n * This type is only used as a base for the DefaultModelDefinition type.\n *\n * @internal\n */\nexport interface WorkspaceModelDefinition {\n [slug: string]: SerializedData\n}\n\n/**\n * Request parameters used by most SDK `list()` methods.\n *\n * @group Request Parameters\n */\nexport interface BaseRequestParameters {\n [key: string]: string | number | boolean | undefined\n /**\n * The page requested.\n */\n page?: number\n /**\n * The limit of items per page.\n */\n limit?: number\n /**\n * A search query string.\n *\n * For instance, searching for \"out\" will match both \"check out!\" and\n * \"about us\". Search is not case-sensitive.\n */\n query?: string\n}\n\n/**\n * Request parameters used by most SDK methods that support advanced querying.\n *\n * @group Request Parameters\n */\nexport interface QueryableRequestParameters {\n /**\n * A search query string that matches a specific word within boundaries.\n * Search is not case-sensitive.\n *\n * For instance, searching for \"phone\" will match \"This is my phone!\"\n * but won't match \"This is my telephone!\".\n */\n 'query:word'?: string\n /**\n * A comma-separated list of fields to look up on when searching using\n * `query` or `query:word`. If undefined, all text fields will be searched on,\n * including Visual Editor fields. Search is not case-sensitive.\n *\n * For instance, to limit search on the \"content\" and \"summary\" fields of a\n * model, pass `'content,summary'`.\n */\n fields?: string\n /**\n * If defined, removes the given item from the list. Useful to create \"related\n * posts\" lists.\n *\n * Note: this field only accepts IDs, and not slugs. Only one ID is allowed.\n */\n except?: number\n}\n"]}
|
package/dist/esm/client.js
CHANGED
|
@@ -56,7 +56,7 @@ import makeCollectionInstance from './instances/Collection';
|
|
|
56
56
|
*/
|
|
57
57
|
export function makeClient(config = {}) {
|
|
58
58
|
var _a, _b, _c;
|
|
59
|
-
let baseUrl = (_a = config.baseUrl) !== null && _a !== void 0 ? _a : 'https://query.
|
|
59
|
+
let baseUrl = (_a = config.baseUrl) !== null && _a !== void 0 ? _a : 'https://query.starlightcms.io/v2';
|
|
60
60
|
let workspace = (_b = config.workspace) !== null && _b !== void 0 ? _b : '';
|
|
61
61
|
let debug = (_c = config.debug) !== null && _c !== void 0 ? _c : false;
|
|
62
62
|
const client = {
|
package/dist/esm/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;;;;;;;AAOA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AACzC,OAAO,iBAAiB,MAAM,mBAAmB,CAAA;AACjD,OAAO,iBAAiB,MAAM,mBAAmB,CAAA;AACjD,OAAO,qBAAqB,MAAM,uBAAuB,CAAA;AACzD,OAAO,sBAAsB,MAAM,wBAAwB,CAAA;AAC3D,OAAO,iBAAiB,MAAM,mBAAmB,CAAA;AACjD,OAAO,kBAAkB,MAAM,oBAAoB,CAAA;AACnD,OAAO,sBAAsB,MAAM,wBAAwB,CAAA;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,UAAU,UAAU,CAGxB,SAA0B,EAAE;;IAE5B,IAAI,OAAO,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;;;;;;;AAOA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AACzC,OAAO,iBAAiB,MAAM,mBAAmB,CAAA;AACjD,OAAO,iBAAiB,MAAM,mBAAmB,CAAA;AACjD,OAAO,qBAAqB,MAAM,uBAAuB,CAAA;AACzD,OAAO,sBAAsB,MAAM,wBAAwB,CAAA;AAC3D,OAAO,iBAAiB,MAAM,mBAAmB,CAAA;AACjD,OAAO,kBAAkB,MAAM,oBAAoB,CAAA;AACnD,OAAO,sBAAsB,MAAM,wBAAwB,CAAA;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,UAAU,UAAU,CAGxB,SAA0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AAO,IAAI,KAAK,CAAC,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,EAAE,IAAI;YACd,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE;gBAC1D,OAAO,iBAAiB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;aACvC;YAED,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QAClC,CAAC;KACF,CAAyD,CAAA;AAC5D,CAAC","sourcesContent":["import {\n DefaultModelDefinition,\n DynamicStarlightClient,\n StarlightClient,\n StarlightConfig,\n WorkspaceModelDefinition,\n} from './types'\nimport { StarlightError } from './errors'\nimport makeModelSelector from './selectors/Model'\nimport makeModelInstance from './instances/Model'\nimport makeSingletonSelector from './selectors/Singleton'\nimport makeCollectionSelector from './selectors/Collection'\nimport makeMediaSelector from './selectors/Media'\nimport makeSearchSelector from './selectors/Search'\nimport makeCollectionInstance from './instances/Collection'\n\n/**\n * Returns a new {@link DynamicStarlightClient}, which is a\n * {@link StarlightClient} with support to create\n * {@apilink ModelInstance | ModelInstances} using the dynamic syntax. To learn\n * which methods a client supports, see {@link StarlightClient}.\n *\n * This function accepts a {@link StarlightConfig} object and can be used to\n * create new clients that connect to a single Starlight workspace. Each client\n * returned by this function is separate and independent of the others.\n *\n * If you only need to make requests to a single workspace, it's probably easier\n * to use the SDK's default client. To use it, you only need to\n * import the default object exported by the SDK:\n *\n * ```ts\n * // `Starlight` below is the default StarlightClient.\n * import Starlight from '@starlightcms/js-sdk'\n * ```\n *\n * See {@link default} for more info on how to use the default client.\n *\n * @example Creating a new client and exporting it.\n * ```ts\n * import { makeStarlightClient } from '@starlightcms/js-sdk'\n *\n * const BlogClient = makeStarlightClient({\n * workspace: '123123123',\n * debug: true\n * })\n *\n * // Feel free to export the new client so your application can use it.\n * export default BlogClient\n * ```\n *\n * @param config The client configuration object. You need to provide at least\n * the `workspace` property. See {@link StarlightConfig} to view all the\n * available options.\n * @group Client\n */\nexport function makeClient<\n D extends WorkspaceModelDefinition = DefaultModelDefinition\n>(\n config: StarlightConfig = {}\n): DynamicStarlightClient<D & WorkspaceModelDefinition> {\n let baseUrl = config.baseUrl ?? 'https://query.starlightcms.io/v2'\n let workspace = config.workspace ?? ''\n let debug = config.debug ?? false\n\n const client: StarlightClient = {\n configure(config) {\n baseUrl = config.baseUrl ?? baseUrl\n workspace = config.workspace ?? workspace\n debug = config.debug ?? debug\n },\n\n log(message, ...optionalParams) {\n if (debug) console.log(message, ...optionalParams)\n },\n\n getBaseUrl() {\n if (!workspace.length) {\n throw new Error(\n 'No workspace defined. Use the `configure()` method on the Starlight client to define one.'\n )\n }\n\n return `${baseUrl}/workspaces/${workspace}`\n },\n\n async get(path, params = {}, options) {\n const filteredParams = Object.keys(params).reduce((accumulator, key) => {\n const param = params[key]\n\n if (param || param === false) accumulator[key] = String(param)\n\n return accumulator\n }, {} as Record<string, string>)\n\n const searchParams = new URLSearchParams(filteredParams).toString()\n const finalPath = searchParams ? `${path}?${searchParams}` : path\n\n this.log(`Starlight - GET ${finalPath}`)\n\n const response = await fetch(`${this.getBaseUrl()}${finalPath}`, options)\n\n if (response.status >= 200 && response.status < 300) {\n return await response.json()\n } else {\n const message = `Starlight - GET ${path} returned ${response.status}: ${response.statusText}`\n throw new StarlightError(message, response)\n }\n },\n\n get models() {\n return makeModelSelector(this)\n },\n\n model(slug) {\n return makeModelInstance(this, slug as string)\n },\n\n get singletons() {\n return makeSingletonSelector(this)\n },\n\n get collections() {\n return makeCollectionSelector(this)\n },\n\n collection(slug: string | number) {\n return makeCollectionInstance(this, slug)\n },\n\n get media() {\n return makeMediaSelector(this)\n },\n\n get search() {\n return makeSearchSelector(this)\n },\n }\n\n return new Proxy(client, {\n get(target, prop) {\n if (typeof prop === 'string' && !Reflect.has(target, prop)) {\n return makeModelInstance(target, prop)\n }\n\n return Reflect.get(target, prop)\n },\n }) as DynamicStarlightClient<D & WorkspaceModelDefinition>\n}\n"]}
|
|
@@ -16,7 +16,7 @@ export * from './selectors';
|
|
|
16
16
|
* provides a default one which can be used by IDEs to provide auto-completion.
|
|
17
17
|
* The default URL points to version 2 of the Query API.
|
|
18
18
|
*/
|
|
19
|
-
export declare type BaseUrl = 'https://query.
|
|
19
|
+
export declare type BaseUrl = 'https://query.starlightcms.io/v2' | string;
|
|
20
20
|
/**
|
|
21
21
|
* The available options to configure a {@link StarlightClient}.
|
|
22
22
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAE3B;;;;GAIG;AACH,oBAAY,OAAO,GAAG
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AACzD,OAAO,EAAE,oBAAoB,EAAE,M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E,MAAM,CAAA;CAChB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AASA,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA","sourcesContent":["import { CollectionEntityTypes, SerializedData } from './entities'\nimport { DynamicModelSelector } from '../selectors/Model'\nimport { DynamicModelInstance } from '../instances/Model'\nimport { SingletonSelector } from '../selectors/Singleton'\nimport { DynamicCollectionSelector } from '../selectors/Collection'\nimport { MediaSelector } from '../selectors/Media'\nimport { SearchSelector } from '../selectors/Search'\nimport { CollectionInstance } from '../instances/Collection'\n\nexport * from './fields'\nexport * from './entities'\nexport * from './visual'\nexport * from './instances'\nexport * from './selectors'\n\n/**\n * This is a utility type that allows any string to be used as a URL, but\n * provides a default one which can be used by IDEs to provide auto-completion.\n * The default URL points to version 2 of the Query API.\n */\nexport type BaseUrl = 'https://query.starlight.sh/v2' | string\n\n/**\n * The available options to configure a {@link StarlightClient}.\n *\n * `workspace` is required when creating new clients or configuring the\n * {@apilink default | default client}.\n * @group Client\n */\nexport type StarlightConfig = {\n /**\n * The ID of the workspace that the client should use to request data.\n *\n * Note: the current APIs only support using workspace IDs as identifiers,\n * and **not** slugs. Slug support will be added in the future. You can find\n * the workspace ID in the left-side menu on the Starlight interface or below\n * the workspace name in the workspace list.\n */\n workspace?: string\n /**\n * The Starlight Query API URL, including the version, and without a trailing\n * slash. Defaults to the production Query API URL.\n *\n * You only need to set this if you're not using Starlight's production APIs.\n */\n baseUrl?: BaseUrl\n /**\n * When true, logs all API requests in the console. Defaults to false.\n */\n debug?: boolean\n}\n\n/**\n * The Starlight client is the main entry point for any requests you might want\n * to make to Starlight's APIs.\n *\n * There's two ways of interacting with a client: using the\n * {@apilink default | default client} exported by the SDK or creating a new\n * client instance using the\n * {@apilink makeStarlightClient | makeStarlightClient function}. Follow the\n * provided links to learn how to use each method.\n *\n * Either way you choose to interact, all clients expose the same methods and\n * accessors that provide ways to request data. These methods and accessors\n * return Selector or Instance objects, which are documented in the sections\n * of the same name found in this API's sidebar.\n *\n * @group Client\n */\nexport interface StarlightClient<\n D extends WorkspaceModelDefinition = DefaultModelDefinition\n> {\n /**\n * Updates the client configuration. See {@link StarlightConfig} to see all\n * the available options.\n *\n * If you want to use the {@apilink default | default SDK client}, you need to\n * set its workspace using this method in your application. You should run\n * this method only once, and as soon as possible in your application\n * lifecycle. For instance, when using React:\n *\n * ```js\n * // src/index.js\n * import { createRoot } from \"react-dom/client\"\n * import Starlight from '@starlightcms/js-sdk'\n * import MyApp from './MyApp.js'\n *\n * // We only need to run this once. In this case, we're\n * // running it before initializing our React app.\n * Starlight.configure({\n * workspace: '1234567890'\n * })\n *\n * const rootElement = document.getElementById(\"root\")\n * const root = createRoot(rootElement)\n *\n * root.render(<MyApp />)\n * ```\n *\n * @param config A configuration object. See {@link StarlightConfig} to view\n * all available options.\n * @category Other Methods\n */\n configure(config: StarlightConfig): void\n\n /**\n * Logs a message (or any kind of data, really) into the console if the debug\n * configuration is true. Uses `console.log` internally.\n *\n * This function's type definition is the same as the `console.log` function.\n *\n * @param message The data that will be logged.\n * @param optionalParams Additional data to be logged or string substitutions.\n * @see [MDN documentation on console.log](https://developer.mozilla.org/en-US/docs/web/api/console/log)\n * @internal\n */\n log(message?: unknown, ...optionalParams: unknown[]): void\n\n /**\n * Returns the base API URL for requests, including the configured workspace.\n * Doesn't include a trailing slash.\n *\n * @internal\n */\n getBaseUrl(): string\n\n /**\n * Returns a Promise that results in a response or throws a StarlightError.\n * The response will be the parsed JSON data sent by the API.\n *\n * This method is used internally by all Selectors and Instances to request\n * data to Starlight's Query API, but it can also be used standalone if\n * desired.\n *\n * This method automatically prefixes the given path with the API URL, but it\n * doesn't include a trailing slash, so be sure to include one at the start\n * of your path.\n *\n * @param path The path to GET. Will be prefixed with the API URL. Must start\n * with a forward slash (/).\n * @param params An optional object of values that will be converted to a\n * string and passed as GET params.\n * @param options An optional configuration object passed to the fetch method.\n * Check [MDN documentation on fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch)\n * to see the available options.\n * @typeParam T - The shape of the expected response on success. Client\n * methods generally pass {@link StarlightItemResponse} or\n * {@link StarlightListResponse} types here.\n * @throws {@link StarlightError} on any errors.\n * @category Other Methods\n */\n get<T = Record<string, unknown>>(\n path: string,\n params?: Record<string, string | number | boolean | undefined>,\n options?: RequestInit\n ): Promise<T>\n\n /**\n * Returns a {@link DynamicModelSelector}, which is a {@link ModelSelector}\n * with support for creating {@apilink ModelInstance | ModelInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.models.list()\n * ```\n *\n * See {@link DynamicModelSelector} for more info.\n *\n * @category Selector Accessors\n */\n get models(): DynamicModelSelector<D>\n\n /**\n * Returns a {@link DynamicModelInstance}, which is a\n * {@link ModelInstance} with support for creating\n * {@apilink ModelCategoryInstance | ModelCategoryInstances} using the dynamic syntax. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.model('posts').entries.list()\n * ```\n *\n * See {@link DynamicModelInstance} for more info.\n *\n * @category Instance Methods\n */\n model<S extends keyof D>(slug: S): DynamicModelInstance<D[S]>\n\n /**\n * Returns a {@link SingletonSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.singletons.list()\n * ```\n *\n * See {@link SingletonSelector} for more info.\n *\n * @category Selector Accessors\n */\n get singletons(): SingletonSelector\n\n /**\n * Returns a {@link DynamicCollectionSelector}, which is a\n * {@link CollectionSelector} with support for creating\n * {@apilink CollectionInstance | CollectionInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collections.list()\n * ```\n *\n * See {@link DynamicCollectionSelector} for more info.\n *\n * @category Selector Accessors\n */\n get collections(): DynamicCollectionSelector\n\n /**\n * Returns a {@link CollectionInstance}. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection('featured-posts').items()\n * ```\n *\n * See {@link CollectionInstance} for more info.\n *\n * @example Typing the returned CollectionInstance.\n * ```ts\n * import Starlight, { MediaObject } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection<MediaObject>('carousel-images').items()\n * ```\n *\n * @typeParam T - The type of entity this Collection holds. You can pass this\n * type parameter to correctly type the response of the\n * {@apilink CollectionInstance.items} method.\n * @category Instance Methods\n */\n collection<T extends CollectionEntityTypes>(\n slug: string | number\n ): CollectionInstance<T>\n\n /**\n * Returns a {@link MediaSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.media.list()\n * ```\n *\n * See {@link MediaSelector} for more info.\n *\n * @category Selector Accessors\n */\n get media(): MediaSelector\n\n /**\n * Returns a {@link SearchSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.search.entries()\n * ```\n *\n * See {@link SearchSelector} for more info.\n *\n * @category Selector Accessors\n */\n get search(): SearchSelector\n}\n\n/**\n * This type adds support for the dynamic syntax to the StarlightClient\n * interface, which allows users to create\n * {@apilink DynamicModelInstance| DynamicModelInstances} dynamically.\n * See {@link StarlightClient} to learn which methods it provides. Also see\n * {@doclink requests-and-responses#dynamic-syntax | Dynamic Instances}\n * documentation to learn more about the dynamic syntax.\n *\n *\n * This allows TypeScript to correctly type all models defined by the user\n * in the DefaultModelDefinition type, aside from letting the user using any\n * \"unknown\" model slug, which is provided by the WorkspaceModelDefinition type.\n *\n * This type is only aware of the DefaultModelDefinition type because the\n * StarlightClient uses it by default when no model definition type is passed.\n *\n * @group Client\n */\nexport type DynamicStarlightClient<T extends WorkspaceModelDefinition> =\n StarlightClient<T> & {\n [K in keyof T]: DynamicModelInstance<T[K]>\n }\n\n/**\n * This interface represents an API response that returns a single entity, like\n * a single {@link Entry} or a single {@link MediaObject}, for instance.\n *\n * It contains only one field: `data`. This field type is generic and\n * depends on the kind of request that was made to the API.\n *\n * All SDK request methods that returna single entity\n * will return this interface.\n *\n * @group Client\n */\nexport interface StarlightItemResponse<T> {\n /**\n * The entity returned by the API request. Its type depends on which request\n * was made. SDK methods will generally type this parameter automatically.\n */\n data: T\n}\n\n/**\n * This interface represents an API response that returns a list of entities,\n * like a list of {@apilink Entry | Entries} or a list of items from a\n * {@link Collection}.\n *\n * It contains a `data` parameter, which is an array of items with a generic\n * type that depends on the kind of request that was made, and two metadata\n * objects: `links` and `meta`. Metadata is useful for pagination.\n *\n * All SDK request methods that return a list of entities will return this\n * interface.\n *\n * @group Client\n */\nexport interface StarlightListResponse<T> {\n /**\n * The list of entities returned by the API request. Its type depends on which\n * request was made. SDK methods will generally type\n * this parameter automatically.\n */\n data: T[]\n /**\n * An object containing useful links for easier pagination. All links points\n * to the same list requested, but with a varying `page` parameter.\n */\n links: {\n /**\n * A link for the first page of the list.\n */\n first: string\n /**\n * A link for the last page of the list.\n */\n last: string\n /**\n * A link to the previous page of the list, if there's any.\n */\n prev?: string\n /**\n * A link to the next page of the list, if there's any.\n */\n next?: string\n }\n /**\n * An object with useful metadata related to the current list. It can be used\n * in applications to create pagination logic and interfaces.\n *\n * `from` and `to` are indexes indicating which items start and finish the\n * current page of the list. For instance, in a list with 100 items with 15\n * items per page, on the first page, `from` and `to` will be `1` and `15`\n * respectively.\n */\n meta: {\n /**\n * The number of the current page.\n */\n current_page: number\n /**\n * The number of the last page for the current list.\n */\n last_page: number\n /**\n * The index of the first item on this page out of all list items.\n */\n from: number\n /**\n * The index of the last item on this page out of all list items.\n */\n to: number\n /**\n * The number of items per page.\n */\n per_page: number\n /**\n * The total number of items in the current list.\n */\n total: number\n }\n}\n\n/**\n * The DefaultModelDefinition type is used to correctly type Entry data when\n * requesting it using a StarlightClient.\n *\n * Without using a DefaultModelDefinition, Entry data will be typed as a\n * \"generic\" JS object which can hold any kind of data:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry> on request success.\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<Record<string, unknown>>. This means that\n * // `data` can hold anything, which is not type-safe.\n * const helloWorld = response.data\n * ```\n *\n * One way of safely adding type information to Entry objects is by passing a\n * data definition type to the EntrySelector when requesting something:\n *\n * ```ts\n * import Starlight, { VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * // response type will now be StarlightItemResponse<Entry<PostsFields>>.\n * const response = await Starlight.posts.entries<PostFields>.get('hello-world')\n *\n * // Now, helloWorld type is Entry<PostsFields>.\n * const helloWorld = response.data\n *\n * // Which means we can safely access its data, and IDEs will\n * // auto-complete the data field name below:\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * Note that we used \"Fields\" to define our data shape. We recommend using these\n * types, which are exported by this SDK, because they map to the field types\n * available in the Starlight admin when creating models, and should simplify\n * the type definition process. All available Field types are documented in the\n * {@apilink BooleanField | Data Fields section of this API}.\n *\n * However, passing model definition types around your application is not ideal,\n * and actually unnecessary. Using the DefaultModelDefinition type, all model\n * definitions can be automatically inferred.\n *\n * To get started, you need to create a\n * [type declaration file](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)\n * in which you'll define all your types. You can name it anything and place it\n * anywhere inside your project, but we recommend naming it `starlight.d.ts` and\n * placing it in the root folder of your source code, so it can be easily\n * imported later in your application if you ever need to reference these types.\n *\n * In this file, you can place all your model type definitions (including type\n * definitions for Singleton data, if you want!). Finally, you just need to\n * add a \"module definition block\" that will tell the SDK which types you expect\n * to receive when requesting models.\n *\n * Here's a complete exemple defining two models, Posts and Magazines:\n *\n * ```ts\n * import { StringField, VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * type MagazineFields = {\n * cover_image: MediaField\n * content: VisualField\n * issue_number: StringField\n * issue_year: StringField\n * }\n *\n * declare module '@starlightcms/js-sdk' {\n * export interface DefaultModelDefinition {\n * // Notice that each key in this interface is a Model slug!\n * posts: PostFields\n * magazines: MagazineFields\n * }\n * }\n * ```\n *\n * After creating this file, types should be automatically inferred without the\n * need to pass these types around:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry<PostsFields>>,\n * // which was implicitly inferred by the SDK!\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<PostsFields>\n * const helloWorld = response.data\n *\n * // Auto-complete will work just as before when we explicitly type the Entry!\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * @group Client\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface DefaultModelDefinition extends WorkspaceModelDefinition {}\n\n/**\n * This type is only used as a base for the DefaultModelDefinition type.\n *\n * @internal\n */\nexport interface WorkspaceModelDefinition {\n [slug: string]: SerializedData\n}\n\n/**\n * Request parameters used by most SDK `list()` methods.\n *\n * @group Request Parameters\n */\nexport interface BaseRequestParameters {\n [key: string]: string | number | boolean | undefined\n /**\n * The page requested.\n */\n page?: number\n /**\n * The limit of items per page.\n */\n limit?: number\n /**\n * A search query string.\n *\n * For instance, searching for \"out\" will match both \"check out!\" and\n * \"about us\". Search is not case-sensitive.\n */\n query?: string\n}\n\n/**\n * Request parameters used by most SDK methods that support advanced querying.\n *\n * @group Request Parameters\n */\nexport interface QueryableRequestParameters {\n /**\n * A search query string that matches a specific word within boundaries.\n * Search is not case-sensitive.\n *\n * For instance, searching for \"phone\" will match \"This is my phone!\"\n * but won't match \"This is my telephone!\".\n */\n 'query:word'?: string\n /**\n * A comma-separated list of fields to look up on when searching using\n * `query` or `query:word`. If undefined, all text fields will be searched on,\n * including Visual Editor fields. Search is not case-sensitive.\n *\n * For instance, to limit search on the \"content\" and \"summary\" fields of a\n * model, pass `'content,summary'`.\n */\n fields?: string\n /**\n * If defined, removes the given item from the list. Useful to create \"related\n * posts\" lists.\n *\n * Note: this field only accepts IDs, and not slugs. Only one ID is allowed.\n */\n except?: number\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AASA,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA","sourcesContent":["import { CollectionEntityTypes, SerializedData } from './entities'\nimport { DynamicModelSelector } from '../selectors/Model'\nimport { DynamicModelInstance } from '../instances/Model'\nimport { SingletonSelector } from '../selectors/Singleton'\nimport { DynamicCollectionSelector } from '../selectors/Collection'\nimport { MediaSelector } from '../selectors/Media'\nimport { SearchSelector } from '../selectors/Search'\nimport { CollectionInstance } from '../instances/Collection'\n\nexport * from './fields'\nexport * from './entities'\nexport * from './visual'\nexport * from './instances'\nexport * from './selectors'\n\n/**\n * This is a utility type that allows any string to be used as a URL, but\n * provides a default one which can be used by IDEs to provide auto-completion.\n * The default URL points to version 2 of the Query API.\n */\nexport type BaseUrl = 'https://query.starlightcms.io/v2' | string\n\n/**\n * The available options to configure a {@link StarlightClient}.\n *\n * `workspace` is required when creating new clients or configuring the\n * {@apilink default | default client}.\n * @group Client\n */\nexport type StarlightConfig = {\n /**\n * The ID of the workspace that the client should use to request data.\n *\n * Note: the current APIs only support using workspace IDs as identifiers,\n * and **not** slugs. Slug support will be added in the future. You can find\n * the workspace ID in the left-side menu on the Starlight interface or below\n * the workspace name in the workspace list.\n */\n workspace?: string\n /**\n * The Starlight Query API URL, including the version, and without a trailing\n * slash. Defaults to the production Query API URL.\n *\n * You only need to set this if you're not using Starlight's production APIs.\n */\n baseUrl?: BaseUrl\n /**\n * When true, logs all API requests in the console. Defaults to false.\n */\n debug?: boolean\n}\n\n/**\n * The Starlight client is the main entry point for any requests you might want\n * to make to Starlight's APIs.\n *\n * There's two ways of interacting with a client: using the\n * {@apilink default | default client} exported by the SDK or creating a new\n * client instance using the\n * {@apilink makeStarlightClient | makeStarlightClient function}. Follow the\n * provided links to learn how to use each method.\n *\n * Either way you choose to interact, all clients expose the same methods and\n * accessors that provide ways to request data. These methods and accessors\n * return Selector or Instance objects, which are documented in the sections\n * of the same name found in this API's sidebar.\n *\n * @group Client\n */\nexport interface StarlightClient<\n D extends WorkspaceModelDefinition = DefaultModelDefinition\n> {\n /**\n * Updates the client configuration. See {@link StarlightConfig} to see all\n * the available options.\n *\n * If you want to use the {@apilink default | default SDK client}, you need to\n * set its workspace using this method in your application. You should run\n * this method only once, and as soon as possible in your application\n * lifecycle. For instance, when using React:\n *\n * ```js\n * // src/index.js\n * import { createRoot } from \"react-dom/client\"\n * import Starlight from '@starlightcms/js-sdk'\n * import MyApp from './MyApp.js'\n *\n * // We only need to run this once. In this case, we're\n * // running it before initializing our React app.\n * Starlight.configure({\n * workspace: '1234567890'\n * })\n *\n * const rootElement = document.getElementById(\"root\")\n * const root = createRoot(rootElement)\n *\n * root.render(<MyApp />)\n * ```\n *\n * @param config A configuration object. See {@link StarlightConfig} to view\n * all available options.\n * @category Other Methods\n */\n configure(config: StarlightConfig): void\n\n /**\n * Logs a message (or any kind of data, really) into the console if the debug\n * configuration is true. Uses `console.log` internally.\n *\n * This function's type definition is the same as the `console.log` function.\n *\n * @param message The data that will be logged.\n * @param optionalParams Additional data to be logged or string substitutions.\n * @see [MDN documentation on console.log](https://developer.mozilla.org/en-US/docs/web/api/console/log)\n * @internal\n */\n log(message?: unknown, ...optionalParams: unknown[]): void\n\n /**\n * Returns the base API URL for requests, including the configured workspace.\n * Doesn't include a trailing slash.\n *\n * @internal\n */\n getBaseUrl(): string\n\n /**\n * Returns a Promise that results in a response or throws a StarlightError.\n * The response will be the parsed JSON data sent by the API.\n *\n * This method is used internally by all Selectors and Instances to request\n * data to Starlight's Query API, but it can also be used standalone if\n * desired.\n *\n * This method automatically prefixes the given path with the API URL, but it\n * doesn't include a trailing slash, so be sure to include one at the start\n * of your path.\n *\n * @param path The path to GET. Will be prefixed with the API URL. Must start\n * with a forward slash (/).\n * @param params An optional object of values that will be converted to a\n * string and passed as GET params.\n * @param options An optional configuration object passed to the fetch method.\n * Check [MDN documentation on fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch)\n * to see the available options.\n * @typeParam T - The shape of the expected response on success. Client\n * methods generally pass {@link StarlightItemResponse} or\n * {@link StarlightListResponse} types here.\n * @throws {@link StarlightError} on any errors.\n * @category Other Methods\n */\n get<T = Record<string, unknown>>(\n path: string,\n params?: Record<string, string | number | boolean | undefined>,\n options?: RequestInit\n ): Promise<T>\n\n /**\n * Returns a {@link DynamicModelSelector}, which is a {@link ModelSelector}\n * with support for creating {@apilink ModelInstance | ModelInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.models.list()\n * ```\n *\n * See {@link DynamicModelSelector} for more info.\n *\n * @category Selector Accessors\n */\n get models(): DynamicModelSelector<D>\n\n /**\n * Returns a {@link DynamicModelInstance}, which is a\n * {@link ModelInstance} with support for creating\n * {@apilink ModelCategoryInstance | ModelCategoryInstances} using the dynamic syntax. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.model('posts').entries.list()\n * ```\n *\n * See {@link DynamicModelInstance} for more info.\n *\n * @category Instance Methods\n */\n model<S extends keyof D>(slug: S): DynamicModelInstance<D[S]>\n\n /**\n * Returns a {@link SingletonSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.singletons.list()\n * ```\n *\n * See {@link SingletonSelector} for more info.\n *\n * @category Selector Accessors\n */\n get singletons(): SingletonSelector\n\n /**\n * Returns a {@link DynamicCollectionSelector}, which is a\n * {@link CollectionSelector} with support for creating\n * {@apilink CollectionInstance | CollectionInstances} using the dynamic syntax.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collections.list()\n * ```\n *\n * See {@link DynamicCollectionSelector} for more info.\n *\n * @category Selector Accessors\n */\n get collections(): DynamicCollectionSelector\n\n /**\n * Returns a {@link CollectionInstance}. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection('featured-posts').items()\n * ```\n *\n * See {@link CollectionInstance} for more info.\n *\n * @example Typing the returned CollectionInstance.\n * ```ts\n * import Starlight, { MediaObject } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.collection<MediaObject>('carousel-images').items()\n * ```\n *\n * @typeParam T - The type of entity this Collection holds. You can pass this\n * type parameter to correctly type the response of the\n * {@apilink CollectionInstance.items} method.\n * @category Instance Methods\n */\n collection<T extends CollectionEntityTypes>(\n slug: string | number\n ): CollectionInstance<T>\n\n /**\n * Returns a {@link MediaSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.media.list()\n * ```\n *\n * See {@link MediaSelector} for more info.\n *\n * @category Selector Accessors\n */\n get media(): MediaSelector\n\n /**\n * Returns a {@link SearchSelector}.\n *\n * This is an accessor, which means that it should be used just like a common\n * object parameter. For instance:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.search.entries()\n * ```\n *\n * See {@link SearchSelector} for more info.\n *\n * @category Selector Accessors\n */\n get search(): SearchSelector\n}\n\n/**\n * This type adds support for the dynamic syntax to the StarlightClient\n * interface, which allows users to create\n * {@apilink DynamicModelInstance| DynamicModelInstances} dynamically.\n * See {@link StarlightClient} to learn which methods it provides. Also see\n * {@doclink requests-and-responses#dynamic-syntax | Dynamic Instances}\n * documentation to learn more about the dynamic syntax.\n *\n *\n * This allows TypeScript to correctly type all models defined by the user\n * in the DefaultModelDefinition type, aside from letting the user using any\n * \"unknown\" model slug, which is provided by the WorkspaceModelDefinition type.\n *\n * This type is only aware of the DefaultModelDefinition type because the\n * StarlightClient uses it by default when no model definition type is passed.\n *\n * @group Client\n */\nexport type DynamicStarlightClient<T extends WorkspaceModelDefinition> =\n StarlightClient<T> & {\n [K in keyof T]: DynamicModelInstance<T[K]>\n }\n\n/**\n * This interface represents an API response that returns a single entity, like\n * a single {@link Entry} or a single {@link MediaObject}, for instance.\n *\n * It contains only one field: `data`. This field type is generic and\n * depends on the kind of request that was made to the API.\n *\n * All SDK request methods that returna single entity\n * will return this interface.\n *\n * @group Client\n */\nexport interface StarlightItemResponse<T> {\n /**\n * The entity returned by the API request. Its type depends on which request\n * was made. SDK methods will generally type this parameter automatically.\n */\n data: T\n}\n\n/**\n * This interface represents an API response that returns a list of entities,\n * like a list of {@apilink Entry | Entries} or a list of items from a\n * {@link Collection}.\n *\n * It contains a `data` parameter, which is an array of items with a generic\n * type that depends on the kind of request that was made, and two metadata\n * objects: `links` and `meta`. Metadata is useful for pagination.\n *\n * All SDK request methods that return a list of entities will return this\n * interface.\n *\n * @group Client\n */\nexport interface StarlightListResponse<T> {\n /**\n * The list of entities returned by the API request. Its type depends on which\n * request was made. SDK methods will generally type\n * this parameter automatically.\n */\n data: T[]\n /**\n * An object containing useful links for easier pagination. All links points\n * to the same list requested, but with a varying `page` parameter.\n */\n links: {\n /**\n * A link for the first page of the list.\n */\n first: string\n /**\n * A link for the last page of the list.\n */\n last: string\n /**\n * A link to the previous page of the list, if there's any.\n */\n prev?: string\n /**\n * A link to the next page of the list, if there's any.\n */\n next?: string\n }\n /**\n * An object with useful metadata related to the current list. It can be used\n * in applications to create pagination logic and interfaces.\n *\n * `from` and `to` are indexes indicating which items start and finish the\n * current page of the list. For instance, in a list with 100 items with 15\n * items per page, on the first page, `from` and `to` will be `1` and `15`\n * respectively.\n */\n meta: {\n /**\n * The number of the current page.\n */\n current_page: number\n /**\n * The number of the last page for the current list.\n */\n last_page: number\n /**\n * The index of the first item on this page out of all list items.\n */\n from: number\n /**\n * The index of the last item on this page out of all list items.\n */\n to: number\n /**\n * The number of items per page.\n */\n per_page: number\n /**\n * The total number of items in the current list.\n */\n total: number\n }\n}\n\n/**\n * The DefaultModelDefinition type is used to correctly type Entry data when\n * requesting it using a StarlightClient.\n *\n * Without using a DefaultModelDefinition, Entry data will be typed as a\n * \"generic\" JS object which can hold any kind of data:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry> on request success.\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<Record<string, unknown>>. This means that\n * // `data` can hold anything, which is not type-safe.\n * const helloWorld = response.data\n * ```\n *\n * One way of safely adding type information to Entry objects is by passing a\n * data definition type to the EntrySelector when requesting something:\n *\n * ```ts\n * import Starlight, { VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * // response type will now be StarlightItemResponse<Entry<PostsFields>>.\n * const response = await Starlight.posts.entries<PostFields>.get('hello-world')\n *\n * // Now, helloWorld type is Entry<PostsFields>.\n * const helloWorld = response.data\n *\n * // Which means we can safely access its data, and IDEs will\n * // auto-complete the data field name below:\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * Note that we used \"Fields\" to define our data shape. We recommend using these\n * types, which are exported by this SDK, because they map to the field types\n * available in the Starlight admin when creating models, and should simplify\n * the type definition process. All available Field types are documented in the\n * {@apilink BooleanField | Data Fields section of this API}.\n *\n * However, passing model definition types around your application is not ideal,\n * and actually unnecessary. Using the DefaultModelDefinition type, all model\n * definitions can be automatically inferred.\n *\n * To get started, you need to create a\n * [type declaration file](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)\n * in which you'll define all your types. You can name it anything and place it\n * anywhere inside your project, but we recommend naming it `starlight.d.ts` and\n * placing it in the root folder of your source code, so it can be easily\n * imported later in your application if you ever need to reference these types.\n *\n * In this file, you can place all your model type definitions (including type\n * definitions for Singleton data, if you want!). Finally, you just need to\n * add a \"module definition block\" that will tell the SDK which types you expect\n * to receive when requesting models.\n *\n * Here's a complete exemple defining two models, Posts and Magazines:\n *\n * ```ts\n * import { StringField, VisualField, MediaField } from '@starlightcms/js-sdk'\n *\n * type PostFields = {\n * featured_image: MediaField\n * content: VisualField\n * }\n *\n * type MagazineFields = {\n * cover_image: MediaField\n * content: VisualField\n * issue_number: StringField\n * issue_year: StringField\n * }\n *\n * declare module '@starlightcms/js-sdk' {\n * export interface DefaultModelDefinition {\n * // Notice that each key in this interface is a Model slug!\n * posts: PostFields\n * magazines: MagazineFields\n * }\n * }\n * ```\n *\n * After creating this file, types should be automatically inferred without the\n * need to pass these types around:\n *\n * ```ts\n * import Starlight from '@starlightcms/js-sdk'\n *\n * // response type will be StarlightItemResponse<Entry<PostsFields>>,\n * // which was implicitly inferred by the SDK!\n * const response = await Starlight.posts.entries.get('hello-world')\n *\n * // helloWorld type is Entry<PostsFields>\n * const helloWorld = response.data\n *\n * // Auto-complete will work just as before when we explicitly type the Entry!\n * console.log(helloWorld.data.featured_image)\n * ```\n *\n * @group Client\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface DefaultModelDefinition extends WorkspaceModelDefinition {}\n\n/**\n * This type is only used as a base for the DefaultModelDefinition type.\n *\n * @internal\n */\nexport interface WorkspaceModelDefinition {\n [slug: string]: SerializedData\n}\n\n/**\n * Request parameters used by most SDK `list()` methods.\n *\n * @group Request Parameters\n */\nexport interface BaseRequestParameters {\n [key: string]: string | number | boolean | undefined\n /**\n * The page requested.\n */\n page?: number\n /**\n * The limit of items per page.\n */\n limit?: number\n /**\n * A search query string.\n *\n * For instance, searching for \"out\" will match both \"check out!\" and\n * \"about us\". Search is not case-sensitive.\n */\n query?: string\n}\n\n/**\n * Request parameters used by most SDK methods that support advanced querying.\n *\n * @group Request Parameters\n */\nexport interface QueryableRequestParameters {\n /**\n * A search query string that matches a specific word within boundaries.\n * Search is not case-sensitive.\n *\n * For instance, searching for \"phone\" will match \"This is my phone!\"\n * but won't match \"This is my telephone!\".\n */\n 'query:word'?: string\n /**\n * A comma-separated list of fields to look up on when searching using\n * `query` or `query:word`. If undefined, all text fields will be searched on,\n * including Visual Editor fields. Search is not case-sensitive.\n *\n * For instance, to limit search on the \"content\" and \"summary\" fields of a\n * model, pass `'content,summary'`.\n */\n fields?: string\n /**\n * If defined, removes the given item from the list. Useful to create \"related\n * posts\" lists.\n *\n * Note: this field only accepts IDs, and not slugs. Only one ID is allowed.\n */\n except?: number\n}\n"]}
|