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.54 kB
{"version":3,"file":"get-single.mjs","names":["documentServices.client.getSingle"],"sources":["../../../../../../src/libs/http/controllers/documents/client/get-single.ts"],"sourcesContent":["import { hoursToSeconds } from \"date-fns\";\nimport { createFactory } from \"hono/factory\";\nimport { describeRoute } from \"hono-openapi\";\nimport z from \"zod\";\nimport { controllerSchemas } from \"../../../../../schemas/documents.js\";\nimport { documentServices } from \"../../../../../services/index.js\";\nimport { LucidAPIError } from \"../../../../../utils/errors/index.js\";\nimport serviceWrapper from \"../../../../../utils/services/service-wrapper.js\";\nimport { copy } from \"../../../../i18n/index.js\";\nimport cacheKeys from \"../../../../kv/cache-keys.js\";\nimport { getCollectionClientScope } from \"../../../../permission/client-scopes.js\";\nimport cache from \"../../../middleware/cache.js\";\nimport clientAuthentication from \"../../../middleware/client-authenticate.js\";\nimport clientScopes from \"../../../middleware/client-scopes.js\";\nimport validate from \"../../../middleware/validate.js\";\nimport openAPI from \"../../../openapi/index.js\";\nimport buildFormattedQuery from \"../../../utils/build-formatted-query.js\";\nimport formatAPIResponse from \"../../../utils/build-response.js\";\nimport createServiceContext from \"../../../utils/create-service-context.js\";\n\nconst factory = createFactory();\n\nconst getSingleController = factory.createHandlers(\n\tdescribeRoute({\n\t\tdescription: \"Get a single document by filters via the client integration.\",\n\t\ttags: [\"client-documents\"],\n\t\tsummary: \"Get Document\",\n\t\tresponses: openAPI.responses({\n\t\t\tschema: z.toJSONSchema(controllerSchemas.client.getSingle.response),\n\t\t}),\n\t\tparameters: openAPI.parameters({\n\t\t\tparams: controllerSchemas.client.getSingle.params,\n\t\t\tquery: controllerSchemas.client.getSingle.query.string,\n\t\t\theaders: {\n\t\t\t\tauthorization: true,\n\t\t\t},\n\t\t}),\n\t}),\n\tclientAuthentication,\n\tvalidate(\"param\", controllerSchemas.client.getSingle.params),\n\tclientScopes((c) => [\n\t\tgetCollectionClientScope(c.req.param(\"collectionKey\") ?? \"\"),\n\t]),\n\tvalidate(\"query\", controllerSchemas.client.getSingle.query.string),\n\tcache({\n\t\tttl: hoursToSeconds(24),\n\t\tmode: \"include-query\",\n\t\tbypass: (c) => c.req.query(\"preview\") !== undefined,\n\t\ttags: (c) => {\n\t\t\tconst collectionKey = c.req.param(\"collectionKey\");\n\t\t\tconst tags: string[] = [cacheKeys.http.tags.clientDocuments];\n\t\t\tif (collectionKey) {\n\t\t\t\ttags.push(cacheKeys.http.tags.clientDocumentsCollection(collectionKey));\n\t\t\t}\n\t\t\treturn tags;\n\t\t},\n\t\tkeyContext: (c) => {\n\t\t\tconst tenantKey = c.get(\"tenant\")?.key;\n\t\t\treturn tenantKey ? { tenant: tenantKey } : {};\n\t\t},\n\t}),\n\tasync (c) => {\n\t\tconst { collectionKey } = c.req.valid(\"param\");\n\t\tconst { preview, version } = c.req.valid(\"query\");\n\n\t\tconst context = createServiceContext(c);\n\t\tconst formattedQuery = await buildFormattedQuery(\n\t\t\tc,\n\t\t\tcontrollerSchemas.client.getSingle.query.formatted,\n\t\t);\n\n\t\tconst document = await serviceWrapper(documentServices.client.getSingle, {\n\t\t\ttransaction: false,\n\t\t\tdefaultError: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tname: copy(\"server:core.routes.document.fetch.error.name\"),\n\t\t\t\tmessage: copy(\"server:core.routes.document.fetch.error.message\"),\n\t\t\t},\n\t\t})(context, {\n\t\t\tcollectionKey,\n\t\t\tversionType: version,\n\t\t\tpreview,\n\t\t\tquery: formattedQuery,\n\t\t\tclientScopes: c.get(\"clientIntegrationAuth\").scopes,\n\t\t});\n\t\tif (document.error) throw new LucidAPIError(document.error);\n\n\t\tif (preview !== undefined) {\n\t\t\tc.header(\"Cache-Control\", \"private, no-store\");\n\t\t\tc.header(\"Pragma\", \"no-cache\");\n\t\t\tc.header(\"Referrer-Policy\", \"no-referrer\");\n\t\t}\n\n\t\tc.status(200);\n\t\treturn c.json(\n\t\t\tformatAPIResponse(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