(req, res)
| 25 | } |
| 26 | |
| 27 | export const apq: Handler = async function (req, res) { |
| 28 | const extensionsRawJson = req.query.get('extensions') |
| 29 | |
| 30 | if (!extensionsRawJson) { |
| 31 | return res.send(400, { |
| 32 | name: 'APQValidation', |
| 33 | error: 'Invalid APQ request', |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | const { persistedQuery } = JSON.parse(extensionsRawJson) as APQExtensions |
| 38 | |
| 39 | if (persistedQuery.version !== 1) { |
| 40 | return res.send(400, { |
| 41 | name: 'APQValidation', |
| 42 | error: 'Unsupported persisted query version', |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | const headers: Record<string, string> = { |
| 47 | [HTTPHeaders.fgScope]: Scope.PUBLIC, |
| 48 | } |
| 49 | |
| 50 | const operationName = req.query.get('operationName') |
| 51 | const authorizationHeader = req.headers.get(HTTPHeaders.authorization) |
| 52 | |
| 53 | const cacheUrl = new URL(req.url) |
| 54 | let cacheKey = '' |
| 55 | |
| 56 | if (operationName) { |
| 57 | cacheKey += operationName |
| 58 | } |
| 59 | |
| 60 | // append "authorization" value to query and make it part of the cache key |
| 61 | if (authorizationHeader) { |
| 62 | headers[HTTPHeaders.fgScope] = Scope.AUTHENTICATED |
| 63 | cacheKey += operationName ? '/' : '' + (await SHA256(authorizationHeader)) |
| 64 | } |
| 65 | |
| 66 | cacheUrl.pathname = cacheUrl.pathname + cacheKey |
| 67 | |
| 68 | const cacheRequest = new Request(cacheUrl.toString(), { |
| 69 | headers: req.headers, |
| 70 | method: 'GET', |
| 71 | }) |
| 72 | |
| 73 | const cache = caches.default |
| 74 | |
| 75 | let response = await cache.match(cacheRequest) |
| 76 | |
| 77 | if (response) { |
| 78 | return response |
| 79 | } |
| 80 | |
| 81 | let query = req.query.get('query') |
| 82 | |
| 83 | const result = await find(persistedQuery.sha256Hash) |
| 84 |
no test coverage detected