-
Notifications
You must be signed in to change notification settings - Fork 84
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can let |
||
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; | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove log?