Skip to content

Commit

Permalink
more jsdoc and make req.respond await
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Mar 19, 2021
1 parent 2d43bde commit e3bcd42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const dec = new TextDecoder()
/**
* Create a new GraphQL HTTP middleware with schema, context etc
* @param {GraphQLOptions} options
*
* @example
* ```ts
* const graphql = await GraphQLHTTP({ schema })
Expand All @@ -18,7 +19,7 @@ export function GraphQLHTTP<Req extends Request = Request, Ctx extends { request
) {
return async (request: Req) => {
if (!['PUT', 'POST', 'PATCH'].includes(request.method)) {
request.respond({
await request.respond({
status: 405,
body: 'Method Not Allowed'
})
Expand All @@ -33,10 +34,10 @@ export function GraphQLHTTP<Req extends Request = Request, Ctx extends { request
request
})

request.respond({ body: JSON.stringify(result, null, 2), status: 200 })
await request.respond({ body: JSON.stringify(result, null, 2), status: 200 })
} catch (e) {
console.error(e)
request.respond({ status: 400, body: 'Malformed request body' })
await request.respond({ status: 400, body: 'Malformed request body' })
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ServerRequest } from 'https://deno.land/[email protected]/http/server.ts'

/**
* Request type with only required properties
*/
export type Request = Pick<ServerRequest, 'respond' | 'body' | 'method'>

0 comments on commit e3bcd42

Please sign in to comment.