|
1 | | -import * as Sentry from '@sentry/cloudflare'; |
2 | 1 | import { env } from 'cloudflare:workers'; |
3 | 2 |
|
4 | 3 | import fetchJson from './fetchJson.ts'; |
5 | 4 | import sortVersions from './sort.ts'; |
| 5 | +import { |
| 6 | + librariesSchema, |
| 7 | + librarySchema, |
| 8 | + libraryVersionSchema, |
| 9 | + libraryVersionSriSchema, |
| 10 | + libraryVersionsSchema, |
| 11 | +} from './kvMetadata.schema.ts'; |
6 | 12 |
|
7 | 13 | const kvBase = env.METADATA_BASE || 'https://metadata.speedcdnjs.com'; |
8 | 14 |
|
9 | 15 | /** |
10 | 16 | * Get a list of libraries. |
11 | | - * |
12 | | - * @return {Promise<string[]>} |
13 | 17 | */ |
14 | | -export const libraries = () => fetchJson(`${kvBase}/packages`); |
15 | | - |
16 | | -/** |
17 | | - * @template {Object} T |
18 | | - * |
19 | | - * Validate the data we get from KV for a library. |
20 | | - * |
21 | | - * @param {string} library Requested library name. |
22 | | - * @param {T} data Returned library data to validate. |
23 | | - * @return {T & { assets: [] }} |
24 | | - */ |
25 | | -const kvLibraryValidate = (library, data) => { |
26 | | - // Assets might not exist if there are none, but we should make it an empty array by default |
27 | | - data.assets = data.assets || []; |
28 | | - |
29 | | - // Non-breaking issues |
30 | | - if (library !== data.name) { |
31 | | - console.info('Name mismatch', library, data.name); |
32 | | - data.name = library; |
33 | | - } |
34 | | - |
35 | | - // Breaking issues |
36 | | - if (!data.version) { |
37 | | - console.error('Version missing', data.name, data); |
38 | | - Sentry.withScope(scope => { |
39 | | - scope.setExtra('data', data); |
40 | | - Sentry.captureException(new Error('Version missing in package data')); |
41 | | - }); |
42 | | - throw new Error('Version missing in package data'); |
43 | | - } |
44 | | - |
45 | | - return data; |
46 | | -}; |
| 18 | +export const libraries = () => |
| 19 | + fetchJson(`${kvBase}/packages`) |
| 20 | + .then(librariesSchema.parse); |
47 | 21 |
|
48 | 22 | /** |
49 | 23 | * Get the metadata for a library. |
50 | 24 | * |
51 | | - * @param {string} name Name of the library to fetch. |
52 | | - * @return {Promise<Object>} |
| 25 | + * @param name Name of the library to fetch. |
53 | 26 | */ |
54 | | -export const library = name => fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}`) |
55 | | - .then(data => kvLibraryValidate(name, data)); |
| 27 | +export const library = (name: string) => |
| 28 | + fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}`) |
| 29 | + .then(res => { |
| 30 | + console.log(res); |
| 31 | + return res; |
| 32 | + }) |
| 33 | + .then(librarySchema.parse) |
| 34 | + .then(res => { |
| 35 | + console.log(res); |
| 36 | + return res; |
| 37 | + }); |
56 | 38 |
|
57 | 39 | /** |
58 | 40 | * Get the versions for a library. |
59 | 41 | * |
60 | | - * @param {string} name Name of the library to fetch. |
61 | | - * @return {Promise<string[]>} |
| 42 | + * @param name Name of the library to fetch. |
62 | 43 | */ |
63 | | -export const libraryVersions = name => fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}/versions`).then(sortVersions); |
| 44 | +export const libraryVersions = (name: string) => |
| 45 | + fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}/versions`) |
| 46 | + .then(libraryVersionsSchema.parse) |
| 47 | + .then(sortVersions); |
64 | 48 |
|
65 | 49 | /** |
66 | 50 | * Get the assets for a library version. |
67 | 51 | * |
68 | | - * @param {string} name Name of the library to fetch. |
69 | | - * @param {string} version Version of the library to fetch. |
70 | | - * @return {Promise<string[]>} |
| 52 | + * @param name Name of the library to fetch. |
| 53 | + * @param version Version of the library to fetch. |
71 | 54 | */ |
72 | | -export const libraryVersion = (name, version) => fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}/versions/${encodeURIComponent(version)}`); |
| 55 | +export const libraryVersion = (name: string, version: string) => |
| 56 | + fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}/versions/${encodeURIComponent(version)}`) |
| 57 | + .then(libraryVersionSchema.parse); |
73 | 58 |
|
74 | 59 | /** |
75 | 60 | * Get the SRI data for a library's version. |
76 | 61 | * |
77 | | - * @param {string} name Name of the library to fetch. |
78 | | - * @param {string} version Version of the library to fetch. |
79 | | - * @return {Promise<Object<string, string>>} |
| 62 | + * @param name Name of the library to fetch. |
| 63 | + * @param version Version of the library to fetch. |
80 | 64 | */ |
81 | | -export const libraryVersionSri = (name, version) => fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}/sris/${encodeURIComponent(version)}`); |
| 65 | +export const libraryVersionSri = (name: string, version: string) => |
| 66 | + fetchJson(`${kvBase}/packages/${encodeURIComponent(name)}/sris/${encodeURIComponent(version)}`) |
| 67 | + .then(libraryVersionSriSchema.parse); |
0 commit comments