File size: 984 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 *  Helper to support nock reply responses using the correct envelope format.
 *  Requests with envelope http_envelope=1 require a specific response format.
 *  @example
 *  const installationWithSuccess = replyWithEnvelope( 200 );
 *  const installationWithError = replyWithEnvelope( 400, { error: 'any error' } );
 *
 *  nock(...).reply(installationWithSuccess() );
 *  nock(...).reply(installationWithError({error: 'any other error'}) );
 */
export const replyWithEnvelope =
	( status: number, defaultBody: Record< string, string | number > = {} ) =>
	( body = {} ) =>
	() => [ status, { code: status, ...defaultBody, ...body } ];

export const replyErrorWithEnvelope =
	( status: number, defaultBody: Record< string, string | number > = {} ) =>
	( body = {} ) =>
	() => [ 200, { code: status, body: { ...defaultBody, ...body } } ];

export const replyWithSuccess = replyWithEnvelope( 200 );
export const replyWithError = replyErrorWithEnvelope( 400, { error: 'any error' } );