Web API for code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

24 lines
652 B

  1. import { FastifyInstance, InjectOptions, LightMyRequestResponse } from 'fastify'
  2. export type MockResponse = LightMyRequestResponse
  3. export type MockResponseThunk = () => Promise<MockResponse>
  4. export type MockClient = (opts: InjectOptions) => Promise<MockResponse>
  5. export const makeJsonRequestClient = (server: FastifyInstance): MockClient => (opts) => {
  6. const inject = {
  7. ...opts,
  8. headers: {
  9. ...(opts.headers ?? {}),
  10. 'accept': 'applicaton/json',
  11. } as Record<string, string>,
  12. }
  13. if (opts.payload) {
  14. inject.headers['content-type'] = 'application/json'
  15. inject.payload = JSON.stringify(opts.payload)
  16. }
  17. return server.inject(inject)
  18. }