Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
feat: export bad token response
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jun 8, 2020
1 parent af68449 commit effc39a
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,41 @@ export const requestHandler = ({
request: http.IncomingMessage,
response: http.ServerResponse,
) => {
let res: Response
if (request.headers.authorization !== `Bearer ${apiKey}`) {
response.writeHead(401, {
'Content-Type': 'application/json; charset=utf-8',
})
response.end(
JSON.stringify({
errors: [
{
code: 'bad_token',
message: 'Bad Token',
},
],
}),
)
res = badTokenResponse()
} else {
res = await p(request.url)
}
const res = await p(request.url)
response.writeHead(res.statusCode, res.headers)
response.end(res.body)
}
}

export const handlePath = (hostname: string) => async (
resource?: string,
): Promise<{
export type Response = {
statusCode: number
headers: { [key: string]: any }
body: string
}> => {
}

export const badTokenResponse = (): Response => ({
statusCode: 401,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
errors: [
{
code: 'bad_token',
message: 'Bad Token',
},
],
}),
})

export const handlePath = (hostname: string) => async (
resource?: string,
): Promise<Response> => {
if (resource === undefined || resource === '/') {
// Start page returns a 404
return {
Expand Down

0 comments on commit effc39a

Please sign in to comment.