import type { Schema } from '../schema/Schema.js'; import type { Agent, Options, Context, Resource } from './Resource.js'; import { resourceDef, ResourceDefinition } from './Resource.js'; import adapter from './Adapter.js'; import ItemResource from './ItemResource.js'; import CollectionResource from './CollectionResource.js'; import { makeStorable } from './StorablePromise.js'; import * as ResourceMethod from './ResourceMethod'; /* Create a REST API loader from an API specification. The API specification is built up from a hierarchy of "resource" specifications. Each resource specification should describe a REST resource of your API (an endpoint, essentially). The actual HTTP requests are delegated to the given *agent*. */ type RestApiOptions = Partial & { agent : Options['agent'] }; const RestApi = Object.assign( >( _options : RestApiOptions, resource : (context : Context) => R ) : R => { //const resource = typeof _resource !== 'function' ? ItemResource(SimpleItem, _resource) : _resource; const options : Options = { adapter, ..._options, }; // Current context while traversing through the resource hierarchy const context = { agent: options.agent, options, path: [], // The current path in the API resource tree store: [], uri: '', // Note: no trailing slash (so empty string in case of empty URI) }; // Return the root item resource return resource(context); }, // Shorthands { Item: ItemResource, Collection: CollectionResource, getResourceConfig: (resource : Resource) => resource[resourceDef], makeStorable: makeStorable, decorateMethod: ResourceMethod.decorateMethod, // Manual method decorator method: ResourceMethod.decorator, // `@decorator` syntax }, ); export default RestApi;