-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor catalog router to use separate modules (#1368)
- Loading branch information
1 parent
1ba834e
commit e8fc5f0
Showing
5 changed files
with
136 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {ApolloClient, InMemoryCache} from '@apollo/client/core/index.js'; | ||
|
||
const CATALOG_GRAPHQL_URL = | ||
process.env['CATALOG_GRAPHQL_URL'] || `http://localhost:6451/graphql`; | ||
|
||
export const client = new ApolloClient({ | ||
uri: CATALOG_GRAPHQL_URL, | ||
cache: new InMemoryCache(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/site-server/src/catalog/routes/catalog/catalog-route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import Router from '@koa/router'; | ||
import {renderPage} from '../../../templates/base.js'; | ||
import {DefaultContext, DefaultState, ParameterizedContext} from 'koa'; | ||
|
||
export const handleCatalogRoute = async ( | ||
context: ParameterizedContext< | ||
DefaultState, | ||
DefaultContext & Router.RouterParamContext<DefaultState, DefaultContext>, | ||
unknown | ||
> | ||
) => { | ||
context.body = renderPage({ | ||
title: `Web Components Catalog`, | ||
content: ` | ||
<h1>Catalog</h1> | ||
`, | ||
}); | ||
context.type = 'html'; | ||
context.status = 200; | ||
}; |
90 changes: 90 additions & 0 deletions
90
packages/site-server/src/catalog/routes/element/element-route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import Router from '@koa/router'; | ||
import {gql} from '@apollo/client/core/index.js'; | ||
import {renderPage} from '../../../templates/base.js'; | ||
import {renderElement} from './element-template.js'; | ||
import {DefaultContext, DefaultState, ParameterizedContext} from 'koa'; | ||
import { client } from '../../graphql.js'; | ||
|
||
export const handleElementRoute = async ( | ||
context: ParameterizedContext< | ||
DefaultState, | ||
DefaultContext & Router.RouterParamContext<DefaultState, DefaultContext>, | ||
unknown | ||
> | ||
) => { | ||
const {params} = context; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const elementPath = params['path']!; | ||
const elementPathSegments = elementPath.split('/'); | ||
const isScoped = elementPathSegments[0]?.startsWith('@'); | ||
const packageName = isScoped | ||
? elementPathSegments[0] + '/' + elementPathSegments[1] | ||
: elementPathSegments[0]!; // eslint-disable-line @typescript-eslint/no-non-null-assertion | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const elementName = elementPathSegments[isScoped ? 2 : 1]!; | ||
|
||
// TODO (justinfagnani): To make this type-safe, we need to write | ||
// a query .graphql document and generate a TypedDocumentNode from it. | ||
const result = await client.query({ | ||
query: gql` | ||
{ | ||
package(packageName: "${packageName}") { | ||
... on ReadablePackageInfo { | ||
name | ||
description | ||
version { | ||
... on ReadablePackageVersion { | ||
version | ||
description | ||
customElements(tagName: "${elementName}") { | ||
tagName | ||
declaration | ||
customElementExport | ||
declaration | ||
} | ||
customElementsManifest | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
if (result.errors !== undefined && result.errors.length > 0) { | ||
throw new Error(result.errors.map((e) => e.message).join('\n')); | ||
} | ||
const {data} = result; | ||
const packageVersion = data.package?.version; | ||
if (packageVersion === undefined) { | ||
throw new Error(`No such package version: ${packageName}`); | ||
} | ||
const customElementsManifest = | ||
packageVersion.customElementsManifest !== undefined && | ||
JSON.parse(packageVersion.customElementsManifest); | ||
|
||
const customElement = packageVersion.customElements?.[0]; | ||
|
||
if (customElement === undefined || customElement.tagName !== elementName) { | ||
throw new Error('Internal error'); | ||
} | ||
|
||
const content = renderElement({ | ||
packageName: packageName, | ||
elementName: elementName, | ||
declarationReference: customElement.declaration, | ||
customElementExport: customElement.export, | ||
manifest: customElementsManifest, | ||
}); | ||
|
||
context.body = renderPage({title: `${packageName}/${elementName}`, content}); | ||
context.type = 'html'; | ||
context.status = 200; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters