|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// This must be imported before lit |
| 8 | +import {renderPage} from '@webcomponents/internal-site-templates/lib/base.js'; |
| 9 | + |
| 10 | +import {DefaultContext, DefaultState, ParameterizedContext} from 'koa'; |
| 11 | +import {Readable} from 'stream'; |
| 12 | +import {gql} from '@apollo/client/core/index.js'; |
| 13 | +import Router from '@koa/router'; |
| 14 | + |
| 15 | +import {renderPackagePage} from '@webcomponents/internal-site-client/lib/pages/package/shell.js'; |
| 16 | +import {client} from '../../graphql.js'; |
| 17 | +import {PackageData} from '@webcomponents/internal-site-client/lib/pages/package/wco-package-page.js'; |
| 18 | +import {marked} from 'marked'; |
| 19 | + |
| 20 | +export const handlePackageRoute = async ( |
| 21 | + context: ParameterizedContext< |
| 22 | + DefaultState, |
| 23 | + DefaultContext & Router.RouterParamContext<DefaultState, DefaultContext>, |
| 24 | + unknown |
| 25 | + > |
| 26 | +) => { |
| 27 | + const {params} = context; |
| 28 | + |
| 29 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 30 | + const packageName = params['name']!; |
| 31 | + |
| 32 | + // TODO (justinfagnani): To make this type-safe, we need to write |
| 33 | + // a query .graphql document and generate a TypedDocumentNode from it. |
| 34 | + const result = await client.query({ |
| 35 | + query: gql`{ |
| 36 | + package(packageName: "${packageName}") { |
| 37 | + ... on ReadablePackageInfo { |
| 38 | + name |
| 39 | + description |
| 40 | + version { |
| 41 | + ... on ReadablePackageVersion { |
| 42 | + version |
| 43 | + description |
| 44 | + customElements { |
| 45 | + tagName |
| 46 | + package |
| 47 | + declaration |
| 48 | + customElementExport |
| 49 | + declaration |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }`, |
| 56 | + }); |
| 57 | + |
| 58 | + if (result.errors !== undefined && result.errors.length > 0) { |
| 59 | + throw new Error(result.errors.map((e) => e.message).join('\n')); |
| 60 | + } |
| 61 | + const {data} = result; |
| 62 | + const packageVersion = data.package?.version; |
| 63 | + if (packageVersion === undefined) { |
| 64 | + // TODO: 404 |
| 65 | + throw new Error(`No such package version: ${packageName}`); |
| 66 | + } |
| 67 | + |
| 68 | + // Set location because wco-nav-bar reads pathname from it. URL isn't |
| 69 | + // exactly a Location, but it's close enough for read-only uses |
| 70 | + globalThis.location = new URL(context.URL.href) as unknown as Location; |
| 71 | + |
| 72 | + const responseData: PackageData = { |
| 73 | + name: packageName, |
| 74 | + description: marked(data.package.description ?? ''), |
| 75 | + version: packageVersion.version, |
| 76 | + elements: packageVersion.customElements, |
| 77 | + }; |
| 78 | + |
| 79 | + context.type = 'html'; |
| 80 | + context.status = 200; |
| 81 | + context.body = Readable.from( |
| 82 | + renderPage( |
| 83 | + { |
| 84 | + title: `${packageName}`, |
| 85 | + initScript: '/js/package/boot.js', |
| 86 | + content: renderPackagePage(responseData), |
| 87 | + initialData: [responseData], |
| 88 | + }, |
| 89 | + { |
| 90 | + // We need to defer elements from hydrating so that we can |
| 91 | + // manually provide data to the element in element-hydrate.js |
| 92 | + deferHydration: true, |
| 93 | + } |
| 94 | + ) |
| 95 | + ); |
| 96 | +}; |
0 commit comments