Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsangan committed Aug 7, 2024
1 parent 07daf44 commit e219b2f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
47 changes: 20 additions & 27 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import { timeNow } from './utils';
import { allowedMethod, timeNow } from './utils';
import serveResult from './helpers';

export default async function checkCache(request: Request): Promise<Response> {
if (request.method === 'GET' || request.method === 'HEAD') {
const now = timeNow();
let cache = caches.default;
let response = await cache.match(request.url);
if (!response) {
response = await serveResult(request);
await cache.put(request.url, response.clone());
}
response = new Response(response.body, response);
response.head
response.headers.delete('Cache-Control');
response.headers.append('X-Response-Time', timeNow() - now);
return response;
} else {
return new Response(
JSON.stringify({
success: false,
message: 'Method not allowed'
}),
{
status: 405,
headers: {
'Content-Type': 'application/json; charset=utf-8',
}
export default async function checkCache(request: Request): Promise <Response> {
if (allowedMethod.indexOf(request.method) === -1) {
return new Response(JSON.stringify({ success: false, message: 'Method not allowed' }), {
status: 405,
headers: {
'Content-Type': 'application/json; charset=utf-8',
}
);
});
}
const now = timeNow();
let cache = caches.default;
let response = await cache.match(request.url);
if (!response) {
response = await serveResult(request);
await cache.put(request.url, response.clone());
}
response = new Response(response.body, response);
response.head
response.headers.delete('Cache-Control');
response.headers.set('X-Response-Time', timeNow() - now);
return response;
}
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getUrl, Result } from './utils';
import { allowedMethod, getUrl, Result } from './utils';
import callAPI from './routing';

export default async function serveResult(request: Request): Promise<Response> {
Expand All @@ -21,7 +21,7 @@ export default async function serveResult(request: Request): Promise<Response> {
status: code,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, HEAD',
'Access-Control-Allow-Methods': allowedMethod.join(', '),
'Access-Control-Expose-Headers': 'X-Response-Time',
'Age': 0,
'CF-Cache-Status': 'MISS',
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const allowedMethod = ['GET', 'HEAD', 'OPTION', 'POST'];

export function getUrl(request: Request): URL {
return new URL(request.url);
}
Expand Down
4 changes: 1 addition & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
name = "validev"
main = "./src/index.ts"
workers_dev = true
compatibility_date = "2022-01-01"
[placement]
mode = "smart"
compatibility_date = "2022-01-01"

0 comments on commit e219b2f

Please sign in to comment.