| 140 | }; |
| 141 | |
| 142 | export const getVersion = async (id: string, req: Request, env: Env) => { |
| 143 | const apiPathname = `/v1/version/${id}`; |
| 144 | const url = new URL(req.url); |
| 145 | url.pathname = apiPathname; |
| 146 | |
| 147 | // Update incoming request to use new pathname |
| 148 | const newRequest = new Request(url.toString(), { |
| 149 | ...req.clone(), |
| 150 | method: 'GET', |
| 151 | }); |
| 152 | const metadata = await env.METADATA.fetch(newRequest); |
| 153 | if (!metadata.ok) { |
| 154 | const error = await metadata.json<StatusErrorObject>(); |
| 155 | throw new StatusError( |
| 156 | metadata.status, |
| 157 | `Bad response from metadata worker. ${error.error}`, |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | return await metadata.json<VersionResponse>(); |
| 162 | }; |