@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 1.7 kB
JavaScript
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../libs/repositories/client-integration-scopes.mjs";import n from"../../libs/repositories/client-integrations.mjs";import{getValidClientScopes as r}from"../../libs/permission/scopes.mjs";import{encodeApiKey as i}from"../../utils/client-integrations/encode-api-key.mjs";import a from"../../utils/client-integrations/generate-keys.mjs";const o=async(o,s)=>{let c=[...new Set(s.scopes)],l=new Set(r(o.config,{tenantKey:o.request.tenantKey})),u=c.filter(e=>!l.has(e));if(u.length>0)return{error:{type:`basic`,name:e(`server:core.client.integrations.scopes.error.name`),message:e(`server:core.client.integrations.scopes.invalid.message`,{data:{invalidScopes:u.join(`, `)}}),status:400},data:void 0};let d=new n(o.db.client,o.config.db),f=new t(o.db.client,o.config.db),{key:p,apiKey:m,apiKeyHash:h,secret:g}=await a(o.config.secrets.encryption),_=await d.selectSingle({select:[`id`],where:[{key:`key`,operator:`=`,value:p}]});if(_.error)return _;if(_.data!==void 0)return{error:{type:`basic`,message:e(`server:core.client.integrations.key.already.exists`),status:400},data:void 0};let v=await d.createSingle({data:{name:s.name,description:s.description,enabled:s.enabled===void 0||s.enabled,key:p,secret:g,api_key:h,tenant_key:o.request.tenantKey??null,created_at:new Date().toISOString(),updated_at:new Date().toISOString()},returning:[`id`,`api_key`],validation:{enabled:!0}});if(v.error)return v;if(c.length>0){let e=await f.createMultiple({data:c.map(e=>({client_integration_id:v.data.id,scope:e,core:!0}))});if(e.error)return e}return{error:void 0,data:{apiKey:i(p,m)}}};export{o as default};
//# sourceMappingURL=create-single.mjs.map