|
| 1 | +import { NuxtConfig as Abstraction } from "~/features/nuxt/abstractions.js"; |
| 2 | +import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js"; |
| 3 | +import { MarkdownContentBuilder } from "~/features/nextjs/MarkdownContentBuilder.js"; |
| 4 | +import { ServiceDiscovery } from "@webiny/api-core/features/serviceDiscovery/index.js"; |
| 5 | +import { ApiKeysRepository } from "@webiny/api-core/features/security/apiKeys/shared/abstractions.js"; |
| 6 | + |
| 7 | +class NuxtConfigImpl implements Abstraction.Interface { |
| 8 | + constructor( |
| 9 | + private tenantContext: TenantContext.Interface, |
| 10 | + private apiKeyRepo: ApiKeysRepository.Interface |
| 11 | + ) {} |
| 12 | + |
| 13 | + async execute(): Abstraction.Return { |
| 14 | + const tenant = this.tenantContext.getTenant(); |
| 15 | + const apiKeyResult = await this.apiKeyRepo.getBySlug("website-builder"); |
| 16 | + const apiKey = apiKeyResult.isOk() ? apiKeyResult.value : null; |
| 17 | + const domains = await this.getDomains(); |
| 18 | + |
| 19 | + const envVars = [ |
| 20 | + `WEBINY_API_KEY={API_TOKEN}`, |
| 21 | + `WEBINY_API_HOST={API_HOST}`, |
| 22 | + `WEBINY_API_TENANT={TENANT_ID}` |
| 23 | + ]; |
| 24 | + |
| 25 | + if (domains.adminHost) { |
| 26 | + envVars.push(`WEBINY_ADMIN_HOST={ADMIN_HOST}`); |
| 27 | + } |
| 28 | + |
| 29 | + const builder = new MarkdownContentBuilder(); |
| 30 | + builder |
| 31 | + .setVariables({ |
| 32 | + DESCRIPTION: `This is a configuration for <a href="{STARTER_KIT_LINK}" target="_blank">Webiny Nuxt starter kit:</a>`, |
| 33 | + STARTER_KIT_LINK: `https://github.com/webiny/website-builder-nuxt`, |
| 34 | + API_TOKEN: apiKey ? apiKey.token : "{API_KEY_TOKEN}", |
| 35 | + API_HOST: domains.apiHost ?? "{API_HOST_URL}", |
| 36 | + ADMIN_HOST: domains.adminHost ?? "{ADMIN_HOST_URL}", |
| 37 | + TENANT_ID: tenant.id |
| 38 | + }) |
| 39 | + .add("description", "{DESCRIPTION}") |
| 40 | + .add("dotEnvStart", "```dotenv") |
| 41 | + .add("dotEnvBody", envVars.join("\n")) |
| 42 | + .add("dotEnvEnd", "```"); |
| 43 | + |
| 44 | + return builder; |
| 45 | + } |
| 46 | + |
| 47 | + private async getDomains() { |
| 48 | + const manifest = await ServiceDiscovery.load(); |
| 49 | + |
| 50 | + const domains: Record<string, string | null> = { |
| 51 | + apiHost: null, |
| 52 | + adminHost: null |
| 53 | + }; |
| 54 | + |
| 55 | + if (!manifest) { |
| 56 | + return domains; |
| 57 | + } |
| 58 | + |
| 59 | + const { api, admin } = manifest; |
| 60 | + |
| 61 | + if (api?.cloudfront) { |
| 62 | + domains.apiHost = api.cloudfront.domain; |
| 63 | + } |
| 64 | + |
| 65 | + if (admin?.cloudfront) { |
| 66 | + domains.adminHost = admin.cloudfront.domain; |
| 67 | + } |
| 68 | + |
| 69 | + return domains; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export const NuxtConfig = Abstraction.createImplementation({ |
| 74 | + implementation: NuxtConfigImpl, |
| 75 | + dependencies: [TenantContext, ApiKeysRepository] |
| 76 | +}); |
0 commit comments