Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Import Package page to catalog #1421

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/site-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
}
},
"dependencies": {
"@lit-labs/task": "^2.0.0",
"@webcomponents/catalog-api": "^0.0.0",
"lit": "^2.6.0",
"lit-analyzer": "^1.2.1"
Expand Down
8 changes: 8 additions & 0 deletions packages/site-client/src/pages/import/boot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import 'lit/experimental-hydrate-support.js';
import './wco-catalog-import-page.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import {html} from 'lit';
import {customElement, state} from 'lit/decorators.js';
import {WCOPage} from '../../shared/wco-page.js';
import {Task} from '@lit-labs/task';

@customElement('wco-catalog-import-page')
export class WCOCatalogImportPage extends WCOPage {
private _importTask = new Task(this, {
task: async ([packageName]: [packageName: string | undefined]) => {
console.log('_importTask', packageName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove log?


if (packageName !== undefined && packageName.trim().length > 0) {
const response = await fetch(`/catalog/import`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
packageName,
}),
});
const result = await response.json();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we need to check the response code before parsing the json? Since fetch doesn't throw on HTTP errors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can let .json() throw and add a error template.

return result;
}
},
args: () => [this._packageName] as [string],
});

@state()
private _packageName?: string;

renderContent() {
return html`
<h1>Import a Package</h1>
<label>Package: <input @change=${this._onPackageNameChange} /></label>
${this._importTask.render({
complete: (value) => html` <h2>Imported</h2>
<pre>${JSON.stringify(value, undefined, 2)}</pre>`,
pending: () => html`<p>Importing package...</p>`,
})}
`;
}

private _onPackageNameChange(e: Event) {
this._packageName = (e.target as HTMLInputElement).value;
}
}

declare global {
interface HTMLElementTagNameMap {
'wco-catalog-import-page': WCOCatalogImportPage;
}
}
2 changes: 2 additions & 0 deletions packages/site-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@
"@webcomponents/internal-site-templates": "^0.0.0",
"google-auth-library": "^8.7.0",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-conditional-get": "^3.0.0",
"koa-etag": "^4.0.0",
"koa-static": "^5.0.0",
"marked": "^4.2.5"
},
"devDependencies": {
"@types/koa-bodyparser": "^4.3.10",
"uvu": "^0.5.6"
}
}
16 changes: 11 additions & 5 deletions packages/site-server/src/lib/catalog/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
*/

import Router from '@koa/router';
import {handleCatalogRoute} from './routes/catalog/catalog-route.js';
import {handleCatalogSearchRoute} from './routes/catalog/search-route.js';
import {handleElementRoute} from './routes/element/element-route.js';
// import cors from '@koa/cors';
import {handleCatalogRoute} from './routes/catalog-page.js';
import {handleCatalogImportRoute} from './routes/import-page.js';
import {handleCatalogImportApiRoute} from './routes/import-api.js';
import {handleElementRoute} from './routes/element-page.js';
import {handleCatalogSearchRoute} from './routes/search-api.js';
import bodyParser from 'koa-bodyparser';

export const catalogRouter = new Router();

// catalogRouter.use(cors());
// Needed for /import
catalogRouter.use(bodyParser());

catalogRouter.get('/', handleCatalogRoute);

catalogRouter.get('/search', handleCatalogSearchRoute);

catalogRouter.get('/import', handleCatalogImportRoute);
catalogRouter.post('/import', handleCatalogImportApiRoute);

catalogRouter.get('/element/:path+', handleElementRoute);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Router from '@koa/router';
import {marked} from 'marked';

import {renderElementPage} from '@webcomponents/internal-site-client/lib/pages/element/shell.js';
import {client} from '../../graphql.js';
import {client} from '../graphql.js';

import type {ElementData} from '@webcomponents/internal-site-client/lib/pages/element/wco-element-page.js';

Expand Down
84 changes: 84 additions & 0 deletions packages/site-server/src/lib/catalog/routes/import-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import {DefaultContext, DefaultState, ParameterizedContext} from 'koa';
import Router from '@koa/router';
import {client} from '../graphql.js';
import {gql} from '@apollo/client/core/index.js';

const importMutation = gql`
mutation ImportPackage($packageName: String!) {
importPackage(packageName: $packageName) {
... on ReadablePackageInfo {
version {
... on ReadablePackageVersion {
version
problems {
code
severity
message
filePath
}
customElements {
tagName
declaration
className
jsExport
}
}
... on UnreadablePackageVersion {
version
status
problems {
code
severity
message
filePath
}
}
}
}
}
}
`;

export const handleCatalogImportApiRoute = async (
context: ParameterizedContext<
DefaultState,
DefaultContext & Router.RouterParamContext<DefaultState, DefaultContext>,
unknown
>
) => {
console.log('context.request.body', context.request.body);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove log

const packageName = (context.request.body as Record<string, string>)[
'packageName'
];
context.type = 'json';

if (typeof packageName !== 'string') {
context.status = 400;
context.body = {
message: 'Body parmeter `packageName` must be a string',
};
return;
}

const result = await client.mutate({
mutation: importMutation,
variables: {packageName},
});

if (result.errors) {
context.status = 500;
context.body = {
message: result.errors,
};
return;
}

context.status = 200;
context.body = result.data;
};
36 changes: 36 additions & 0 deletions packages/site-server/src/lib/catalog/routes/import-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright 2022 Google LLC
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
* SPDX-License-Identifier: Apache-2.0
*/

// This must be imported before lit
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
import {renderPage} from '@webcomponents/internal-site-templates/lib/base.js';
import {DefaultContext, DefaultState, ParameterizedContext} from 'koa';
import {html} from 'lit';
import {Readable} from 'stream';
import Router from '@koa/router';

import '@webcomponents/internal-site-client/lib/pages/import/wco-catalog-import-page.js';

export const handleCatalogImportRoute = async (
context: ParameterizedContext<
DefaultState,
DefaultContext & Router.RouterParamContext<DefaultState, DefaultContext>,
unknown
>
) => {
// Set location because wco-nav-bar reads pathname from it. URL isn't
// exactly a Location, but it's close enough for read-only uses
globalThis.location = new URL(context.URL.href) as unknown as Location;

context.body = Readable.from(
renderPage({
title: `Web Components Catalog - Import Package`,
initScript: '/js/import/boot.js',
content: html`<wco-catalog-import-page></wco-catalog-import-page>`,
})
);
context.type = 'html';
context.status = 200;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {DefaultContext, DefaultState, ParameterizedContext} from 'koa';
import Router from '@koa/router';
import {client} from '../../graphql.js';
import {client} from '../graphql.js';
import {gql} from '@apollo/client/core/index.js';

const elementsQuery = gql`
Expand Down