UNPKG

@lokalise/api-contracts

Version:
50 lines 1.94 kB
import { SUCCESSFUL_HTTP_STATUS_CODES } from '../HttpStatusCodes.js'; import { isContentResponseEntry, isSseBody, } from './contractResponse.js'; export const defineApiContract = (contract) => ({ ...contract }); export const mapApiContractToPath = (routeConfig) => { if (!routeConfig.requestPathParamsSchema) { return routeConfig.pathResolver(undefined); } const resolverParams = Object.keys(routeConfig.requestPathParamsSchema.shape).reduce((acc, key) => { acc[key] = `:${key}`; return acc; }, {}); return routeConfig.pathResolver(resolverParams); }; export const describeApiContract = (routeConfig) => { return `${routeConfig.method.toUpperCase()} ${mapApiContractToPath(routeConfig)}`; }; /** Collects every SSE event-schema map declared by a single response entry (legacy or content-map). */ const collectSseSchemaMaps = (value) => { if (isContentResponseEntry(value)) { const maps = []; for (const descriptor of Object.values(value.content ?? {})) { if (isSseBody(descriptor)) { maps.push(descriptor.schemaByEventName); } } return maps; } return []; }; export const getSseSchemaByEventName = (routeConfig) => { const result = {}; for (const value of Object.values(routeConfig.responsesByStatusCode)) { if (value) { for (const map of collectSseSchemaMaps(value)) { Object.assign(result, map); } } } return Object.keys(result).length > 0 ? result : null; }; export const hasAnySuccessSseResponse = (apiContract) => { for (const code of [...SUCCESSFUL_HTTP_STATUS_CODES, '2xx', 'default']) { const value = apiContract.responsesByStatusCode[code]; if (value && collectSseSchemaMaps(value).length > 0) { return true; } } return false; }; //# sourceMappingURL=defineApiContract.js.map