UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 5.86 kB
{"version":3,"file":"responses.mjs","names":["constants"],"sources":["../../../../src/libs/http/openapi/responses.ts"],"sourcesContent":["import type { OpenAPIV3 } from \"openapi-types\";\nimport constants from \"../../../constants/constants.js\";\nimport { translate } from \"../../i18n/index.js\";\n\nconst metaObject: OpenAPIV3.SchemaObject = {\n\ttype: \"object\",\n\tproperties: {\n\t\tpath: { type: \"string\" },\n\t\tlinks: { type: \"array\", items: {} },\n\t\tcurrentPage: { type: \"number\", nullable: true, example: null },\n\t\tlastPage: { type: \"number\", nullable: true, example: null },\n\t\tperPage: { type: \"number\", nullable: true, example: null },\n\t\ttotal: { type: \"number\", nullable: true, example: null },\n\t},\n};\n\nconst paginatedMetaObject: OpenAPIV3.SchemaObject = {\n\ttype: \"object\",\n\tproperties: {\n\t\tpath: { type: \"string\" },\n\t\tlinks: {\n\t\t\ttype: \"array\",\n\t\t\titems: {\n\t\t\t\ttype: \"object\",\n\t\t\t\tproperties: {\n\t\t\t\t\tactive: { type: \"boolean\" },\n\t\t\t\t\tlabel: { type: \"string\" },\n\t\t\t\t\turl: { type: \"string\", nullable: true },\n\t\t\t\t\tpage: { type: \"number\" },\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tcurrentPage: {\n\t\t\ttype: \"number\",\n\t\t\tnullable: true,\n\t\t\texample: constants.query.page,\n\t\t},\n\t\tlastPage: {\n\t\t\ttype: \"number\",\n\t\t\tnullable: true,\n\t\t\texample: 200 / constants.query.perPage,\n\t\t},\n\t\tperPage: {\n\t\t\ttype: \"number\",\n\t\t\tnullable: true,\n\t\t\texample: constants.query.perPage,\n\t\t},\n\t\ttotal: { type: \"number\", nullable: true, example: 200 },\n\t},\n};\n\nconst linksObject: OpenAPIV3.SchemaObject = {\n\ttype: \"object\",\n\tproperties: {\n\t\tfirst: { type: \"string\", nullable: true },\n\t\tlast: { type: \"string\", nullable: true },\n\t\tnext: { type: \"string\", nullable: true },\n\t\tprev: { type: \"string\", nullable: true },\n\t},\n};\n\nexport const defaultErrorResponse = {\n\ttype: \"object\",\n\tdescription: translate(\"server:core.openapi.response.default\"),\n\tproperties: {\n\t\tstatus: {\n\t\t\ttype: \"number\",\n\t\t\tnullable: true,\n\t\t},\n\t\tcode: {\n\t\t\ttype: \"string\",\n\t\t\tnullable: true,\n\t\t},\n\t\tkey: {\n\t\t\ttype: \"string\",\n\t\t\tnullable: true,\n\t\t},\n\t\tname: {\n\t\t\ttype: \"string\",\n\t\t\tnullable: true,\n\t\t},\n\t\tmessage: {\n\t\t\ttype: \"string\",\n\t\t\tnullable: true,\n\t\t},\n\t\terrors: {\n\t\t\ttype: \"object\",\n\t\t\tnullable: true,\n\t\t\tadditionalProperties: true,\n\t\t},\n\t},\n\tadditionalProperties: true,\n} as const;\n\n/**\n * Used to construct a response object for OpenAPI\n */\nconst responses = (config?: {\n\tschema?: unknown;\n\tpaginated?: boolean;\n\tnoProperties?: boolean;\n}) => {\n\tconst response: Record<\n\t\tstring,\n\t\t| Omit<OpenAPIV3.ResponseObject, \"headers\" | \"context\" | \"links\">\n\t\t| OpenAPIV3.ReferenceObject\n\t> = {};\n\n\tif (config?.schema) {\n\t\tresponse[200] = {\n\t\t\tdescription: translate(\"server:core.openapi.response.200\"),\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema:\n\t\t\t\t\t\tconfig.noProperties === true\n\t\t\t\t\t\t\t? { type: \"object\", nullable: true }\n\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t\t\tdata: config.schema,\n\t\t\t\t\t\t\t\t\t\tmeta: config.paginated ? paginatedMetaObject : metaObject,\n\t\t\t\t\t\t\t\t\t\t...(config.paginated ? { links: linksObject } : {}),\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t} else {\n\t\tresponse[204] = {\n\t\t\tdescription: translate(\"server:core.openapi.response.204\"),\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tnullable: true,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\n\tresponse.default = {\n\t\tdescription: translate(\"server:core.openapi.response.default\"),\n\t\tcontent: {\n\t\t\t\"application/json\": {\n\t\t\t\tschema: defaultErrorResponse,\n\t\t\t},\n\t\t},\n\t};\n\n\treturn response;\n};\n\nexport default responses;\n"],"mappings":"qGAIA,MAAM,EAAqC,CAC1C,KAAM,SACN,WAAY,CACX,KAAM,CAAE,KAAM,QAAS,EACvB,MAAO,CAAE,KAAM,QAAS,MAAO,CAAC,CAAE,EAClC,YAAa,CAAE,KAAM,SAAU,SAAU,GAAM,QAAS,IAAK,EAC7D,SAAU,CAAE,KAAM,SAAU,SAAU,GAAM,QAAS,IAAK,EAC1D,QAAS,CAAE,KAAM,SAAU,SAAU,GAAM,QAAS,IAAK,EACzD,MAAO,CAAE,KAAM,SAAU,SAAU,GAAM,QAAS,IAAK,CACxD,CACD,EAEM,EAA8C,CACnD,KAAM,SACN,WAAY,CACX,KAAM,CAAE,KAAM,QAAS,EACvB,MAAO,CACN,KAAM,QACN,MAAO,CACN,KAAM,SACN,WAAY,CACX,OAAQ,CAAE,KAAM,SAAU,EAC1B,MAAO,CAAE,KAAM,QAAS,EACxB,IAAK,CAAE,KAAM,SAAU,SAAU,EAAK,EACtC,KAAM,CAAE,KAAM,QAAS,CACxB,CACD,CACD,EACA,YAAa,CACZ,KAAM,SACN,SAAU,GACV,QAASA,EAAU,MAAM,IAC1B,EACA,SAAU,CACT,KAAM,SACN,SAAU,GACV,QAAS,IAAMA,EAAU,MAAM,OAChC,EACA,QAAS,CACR,KAAM,SACN,SAAU,GACV,QAASA,EAAU,MAAM,OA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