Skip to content

Commit

Permalink
Allowed Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsangan committed Aug 8, 2024
1 parent 992f16a commit 1d8ac20
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
48 changes: 24 additions & 24 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
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.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',
}
const now = timeNow();
if (allowedMethod.indexOf(request.method) === -1) {
return new Response(JSON.stringify({
success: false,
message: 'Method not allowed'
}), {
status: 405,
headers: {
'Allow': allowedMethod.join(', '),
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': allowedMethod.join(', '),
'Content-Type': 'application/json; charset=utf-8',
'X-Powered-By': '@ihsangan/valid'
}
);
});
}
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.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 { getUrl, Result, allowedMethod } 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': 'Age, X-Powered-By, X-Response-Time',
'Age': 0,
'CF-Cache-Status': 'MISS',
Expand Down
6 changes: 4 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const endpoint = 'https://order-sg.codashop.com/initPayment.action';

export const allowedMethod = ['GET', 'HEAD'];

export function getUrl(request: Request): URL {
return new URL(request.url);
}
Expand All @@ -6,8 +10,6 @@ export function timeNow(): number {
return Date.now();
}

export const endpoint = 'https://order-sg.codashop.com/initPayment.action';

export const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
Expand Down

0 comments on commit 1d8ac20

Please sign in to comment.