-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotify-api-client.ts
More file actions
120 lines (110 loc) · 3.28 KB
/
notify-api-client.ts
File metadata and controls
120 lines (110 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import axios, { AxiosInstance, isAxiosError } from 'axios';
import type { AxiosError } from 'axios';
import type { Readable } from 'node:stream';
import { constants as HTTP2_CONSTANTS } from 'node:http2';
import type {
SingleMessageErrorResponse,
SingleMessageRequest,
SingleMessageResponse,
} from 'domain/request';
import { RequestAlreadyReceivedError } from 'domain/request-already-received-error';
import { RetryConfig, conditionalRetry } from 'utils';
import type { Logger } from 'utils';
import { RequestNotifyError } from 'domain/request-notify-error';
export interface IAccessTokenRepository {
getAccessToken(): Promise<string>;
}
export type Response = {
data: Readable;
};
export interface INotifyClient {
sendRequest(
apiRequest: SingleMessageRequest,
correlationId?: string,
): Promise<SingleMessageResponse>;
}
/*
* Client for sending requests to the NHS Notify API see
* https://digital.nhs.uk/developer/api-catalogue/nhs-notify
*/
export class NotifyClient implements INotifyClient {
private client: AxiosInstance;
constructor(
private apimBaseUrl: string,
private accessTokenRepository: IAccessTokenRepository,
private logger: Logger,
private backoffConfig: RetryConfig = {
maxDelayMs: 10_000,
intervalMs: 1000,
exponentialRate: 2,
maxAttempts: 10,
},
) {
this.client = axios.create({
baseURL: this.apimBaseUrl,
});
}
public async sendRequest(
apiRequest: SingleMessageRequest,
correlationId: string,
): Promise<SingleMessageResponse> {
try {
return await conditionalRetry(
async (attempt) => {
const accessToken = await this.accessTokenRepository.getAccessToken();
this.logger.debug({
correlationId,
description: 'Sending request',
attempt,
});
const headers = {
'Content-Type': 'application/json',
'X-Correlation-ID': correlationId,
...(accessToken === ''
? {}
: {
Authorization: `Bearer ${accessToken}`,
}),
};
const response = await this.client.post<SingleMessageResponse>(
'/comms/v1/messages',
apiRequest,
{ headers },
);
return response.data;
},
(err) =>
Boolean(
isAxiosError(err) &&
err.response?.status ===
HTTP2_CONSTANTS.HTTP_STATUS_TOO_MANY_REQUESTS,
),
this.backoffConfig,
);
} catch (error: any) {
this.logger.error({
description: 'Failed sending request to Notify',
err: error,
});
if (isAxiosError(error)) {
const axiosError: AxiosError = error;
if (
axiosError.response?.status ===
HTTP2_CONSTANTS.HTTP_STATUS_UNPROCESSABLE_ENTITY
) {
throw new RequestAlreadyReceivedError(error, correlationId);
} else {
const errorBody: SingleMessageErrorResponse = axiosError.response
?.data as SingleMessageErrorResponse;
throw new RequestNotifyError(
error,
correlationId,
errorBody?.errors[0].code,
errorBody?.errors[0].detail,
);
}
}
throw error;
}
}
}