@slango/ristretto 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +7 -0
  3. package/LICENSE +201 -0
  4. package/README.md +1 -0
  5. package/dist/abortable/index.d.ts +18 -0
  6. package/dist/abortable/index.d.ts.map +1 -0
  7. package/dist/abortable/index.js +22 -0
  8. package/dist/abortable/index.js.map +1 -0
  9. package/dist/index.d.ts +15 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +14 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/middleware/index.d.ts +12 -0
  14. package/dist/middleware/index.d.ts.map +1 -0
  15. package/dist/middleware/index.js +2 -0
  16. package/dist/middleware/index.js.map +1 -0
  17. package/dist/middleware/runner.d.ts +12 -0
  18. package/dist/middleware/runner.d.ts.map +1 -0
  19. package/dist/middleware/runner.js +48 -0
  20. package/dist/middleware/runner.js.map +1 -0
  21. package/dist/utils/HttpError.d.ts +5 -0
  22. package/dist/utils/HttpError.d.ts.map +1 -0
  23. package/dist/utils/HttpError.js +9 -0
  24. package/dist/utils/HttpError.js.map +1 -0
  25. package/dist/utils/abortableRequest.d.ts +7 -0
  26. package/dist/utils/abortableRequest.d.ts.map +1 -0
  27. package/dist/utils/abortableRequest.js +12 -0
  28. package/dist/utils/abortableRequest.js.map +1 -0
  29. package/dist/utils/auth.d.ts +4 -0
  30. package/dist/utils/auth.d.ts.map +1 -0
  31. package/dist/utils/auth.js +8 -0
  32. package/dist/utils/auth.js.map +1 -0
  33. package/dist/utils/baseUrl.d.ts +5 -0
  34. package/dist/utils/baseUrl.d.ts.map +1 -0
  35. package/dist/utils/baseUrl.js +8 -0
  36. package/dist/utils/baseUrl.js.map +1 -0
  37. package/dist/utils/body.d.ts +16 -0
  38. package/dist/utils/body.d.ts.map +1 -0
  39. package/dist/utils/body.js +37 -0
  40. package/dist/utils/body.js.map +1 -0
  41. package/dist/utils/httpMethod.d.ts +12 -0
  42. package/dist/utils/httpMethod.d.ts.map +1 -0
  43. package/dist/utils/httpMethod.js +13 -0
  44. package/dist/utils/httpMethod.js.map +1 -0
  45. package/dist/utils/queryParams.d.ts +8 -0
  46. package/dist/utils/queryParams.d.ts.map +1 -0
  47. package/dist/utils/queryParams.js +19 -0
  48. package/dist/utils/queryParams.js.map +1 -0
  49. package/dist/utils/request.d.ts +15 -0
  50. package/dist/utils/request.d.ts.map +1 -0
  51. package/dist/utils/request.js +28 -0
  52. package/dist/utils/request.js.map +1 -0
  53. package/dist/utils/status.d.ts +3 -0
  54. package/dist/utils/status.d.ts.map +1 -0
  55. package/dist/utils/status.js +13 -0
  56. package/dist/utils/status.js.map +1 -0
  57. package/eslint.config.js +1 -0
  58. package/lint-staged.config.js +1 -0
  59. package/package.json +46 -0
  60. package/src/abortable/index.ts +48 -0
  61. package/src/index.ts +34 -0
  62. package/src/middleware/index.ts +20 -0
  63. package/src/middleware/runner.spec.ts +174 -0
  64. package/src/middleware/runner.ts +65 -0
  65. package/src/utils/HttpError.ts +9 -0
  66. package/src/utils/abortableRequest.spec.ts +50 -0
  67. package/src/utils/abortableRequest.ts +22 -0
  68. package/src/utils/auth.spec.ts +58 -0
  69. package/src/utils/auth.ts +16 -0
  70. package/src/utils/baseUrl.ts +16 -0
  71. package/src/utils/body.spec.ts +123 -0
  72. package/src/utils/body.ts +74 -0
  73. package/src/utils/httpMethod.ts +11 -0
  74. package/src/utils/queryParams.spec.ts +75 -0
  75. package/src/utils/queryParams.ts +31 -0
  76. package/src/utils/request.spec.ts +180 -0
  77. package/src/utils/request.ts +57 -0
  78. package/src/utils/status.spec.ts +66 -0
  79. package/src/utils/status.ts +15 -0
  80. package/tsconfig.build.json +10 -0
  81. package/tsconfig.build.tsbuildinfo +1 -0
  82. package/tsconfig.json +9 -0
  83. package/vitest.config.js +1 -0
@@ -0,0 +1,16 @@
1
+ import { AbortablePromise } from './abortableRequest.js';
2
+ import { Request, RequestOptionsWithoutUrl } from './request.js';
3
+
4
+ export const withBearerToken =
5
+ (bearerToken: string) =>
6
+ <Response, PromiseType extends AbortablePromise<Response> | Promise<Response>>(
7
+ req: Request<Response, PromiseType>,
8
+ ) =>
9
+ (options: Omit<RequestOptionsWithoutUrl, 'body'> = {}) =>
10
+ req({
11
+ ...options,
12
+ headers: {
13
+ ...options.headers,
14
+ Authorization: `Bearer ${bearerToken}`,
15
+ },
16
+ });
@@ -0,0 +1,16 @@
1
+ type AbsoluteURLString = `${Lowercase<'http' | 'https'>}://${string}`;
2
+
3
+ export type BaseURL = AbsoluteURLString | URL;
4
+
5
+ export const getDefaultBaseUrl = (): string => {
6
+ const baseUrl =
7
+ typeof window === 'undefined' ? process.env.BASE_URL || '' : window.location.origin;
8
+
9
+ if (!baseUrl) {
10
+ throw new Error('BASE_URL is not defined');
11
+ }
12
+
13
+ return baseUrl;
14
+ };
15
+
16
+ // TODO: We neeed a withBaseUrl helper
@@ -0,0 +1,123 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { patch, post, put } from '../index.js';
4
+ import { JsonObjectWithFile, withFormDataBody, withJsonBody } from './body.js';
5
+ import { HttpMethod } from './httpMethod.js';
6
+ import { request } from './request.js';
7
+
8
+ vi.mock('./request.js', () => ({
9
+ request: vi.fn(),
10
+ }));
11
+
12
+ const requestMock = vi.mocked(request);
13
+
14
+ const postRequest = post('/api/data/post');
15
+ const putRequest = put('/api/data/put');
16
+ const patchRequest = patch('/api/data/patch');
17
+
18
+ afterEach(() => {
19
+ vi.clearAllMocks();
20
+ });
21
+
22
+ const formDataToObject = (formData: FormData) => {
23
+ const obj: JsonObjectWithFile = {};
24
+ for (const [key, value] of formData.entries()) {
25
+ if (obj[key]) {
26
+ obj[key] = Array.isArray(obj[key]) ? [...obj[key], value] : [obj[key], value];
27
+ } else {
28
+ obj[key] = value;
29
+ }
30
+ }
31
+ return obj;
32
+ };
33
+
34
+ describe('withJsonBody', () => {
35
+ it('should set JSON body and content-type header', async () => {
36
+ const jsonBody = { key: 'value' };
37
+ await withJsonBody(jsonBody)(postRequest)();
38
+ const requestBody = requestMock.mock.calls[0][1].body as string;
39
+
40
+ expect(requestBody).toBe(JSON.stringify(jsonBody));
41
+ });
42
+
43
+ it('should merge existing headers', async () => {
44
+ const jsonBody = { key: 'value' };
45
+ await withJsonBody(jsonBody)(putRequest)({ headers: { Authorization: 'Bearer token' } });
46
+
47
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.PUT, {
48
+ body: JSON.stringify(jsonBody),
49
+ headers: {
50
+ Authorization: 'Bearer token',
51
+ 'content-type': 'application/json',
52
+ },
53
+ url: '/api/data/put',
54
+ });
55
+ });
56
+ });
57
+
58
+ describe('withFormDataBody', () => {
59
+ it('should set FormData body with simple key-value pairs', async () => {
60
+ const body = { key: 'value' };
61
+ await withFormDataBody(body)(patchRequest)();
62
+ const requestBody = requestMock.mock.calls[0][1].body as FormData;
63
+
64
+ const formData = new FormData();
65
+ formData.append('key', 'value');
66
+
67
+ expect(formDataToObject(requestBody)).toEqual(formDataToObject(formData));
68
+ });
69
+
70
+ it('should handle nested objects in FormData', async () => {
71
+ const body = { user: { age: 25, name: 'Alice' } };
72
+ await withFormDataBody(body)(postRequest)();
73
+ const requestBody = requestMock.mock.calls[0][1].body as FormData;
74
+
75
+ const formData = new FormData();
76
+ formData.append('user[name]', 'Alice');
77
+ formData.append('user[age]', '25');
78
+
79
+ expect(formDataToObject(requestBody)).toEqual(formDataToObject(formData));
80
+ });
81
+
82
+ it('should handle arrays in FormData', async () => {
83
+ const body = { tags: ['tag1', 'tag2'] };
84
+ await withFormDataBody(body)(putRequest)();
85
+ const requestBody = requestMock.mock.calls[0][1].body as FormData;
86
+
87
+ const formData = new FormData();
88
+ formData.append('tags[0]', 'tag1');
89
+ formData.append('tags[1]', 'tag2');
90
+
91
+ expect(formDataToObject(requestBody)).toEqual(formDataToObject(formData));
92
+ });
93
+
94
+ it('should handle File and Blob objects in FormData', async () => {
95
+ const file = new File(['file contents'], 'test.txt');
96
+ const body = { file };
97
+ await withFormDataBody(body)(patchRequest)();
98
+ const requestBody = requestMock.mock.calls[0][1].body as FormData;
99
+
100
+ const formData = new FormData();
101
+ formData.append('file', file);
102
+
103
+ expect(formDataToObject(requestBody)).toEqual(formDataToObject(formData));
104
+ });
105
+
106
+ it('should merge existing headers', async () => {
107
+ const jsonBody = { key: 'value' };
108
+ await withFormDataBody(jsonBody)(putRequest)({ headers: { Authorization: 'Bearer token' } });
109
+ const requestBody = requestMock.mock.calls[0][1].body as FormData;
110
+
111
+ const formData = new FormData();
112
+ formData.append('key', 'value');
113
+
114
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.PUT, {
115
+ body: expect.any(FormData) as FormData,
116
+ headers: {
117
+ Authorization: 'Bearer token',
118
+ },
119
+ url: '/api/data/put',
120
+ });
121
+ expect(formDataToObject(requestBody)).toEqual(formDataToObject(formData));
122
+ });
123
+ });
@@ -0,0 +1,74 @@
1
+ import { JsonPrimitive as BaseJsonPrimitive, JsonObject, JsonValue } from 'type-fest';
2
+
3
+ import { AbortablePromise } from './abortableRequest.js';
4
+ import { Request, RequestOptionsWithoutUrl } from './request.js';
5
+
6
+ // undefined is not allowed in JsonObjectWithFile (because it is not allowed in Json)
7
+ export type JsonObjectWithFile = {
8
+ [Key in string]: JsonValueWithFile;
9
+ } & {
10
+ [Key in string]?: JsonValueWithFile;
11
+ };
12
+ type JsonArrayWithFile = JsonValueWithFile[] | readonly JsonValueWithFile[];
13
+ type JsonPrimitiveWithFile = BaseJsonPrimitive | Blob | File;
14
+ type JsonValueWithFile = JsonArrayWithFile | JsonObjectWithFile | JsonPrimitiveWithFile;
15
+
16
+ const isJsonObject = (value: unknown): value is JsonObject =>
17
+ typeof value === 'object' && !Array.isArray(value);
18
+
19
+ const jsonToFormData = (
20
+ json: JsonObjectWithFile,
21
+ formData = new FormData(),
22
+ parentKey = '',
23
+ ): FormData => {
24
+ const appendValue = (key: string, value: unknown) => {
25
+ if (value instanceof File || value instanceof Blob) {
26
+ formData.append(key, value);
27
+ } else if (isJsonObject(value)) {
28
+ // Recursive call for nested objects
29
+ jsonToFormData(value, formData, key);
30
+ } else {
31
+ formData.append(key, value as Blob | string);
32
+ }
33
+ };
34
+
35
+ Object.entries(json).forEach(([key, value]) => {
36
+ const fullKey = parentKey ? `${parentKey}[${key}]` : key;
37
+
38
+ if (Array.isArray(value)) {
39
+ value.forEach((item, index) => appendValue(`${fullKey}[${index}]`, item));
40
+ } else {
41
+ appendValue(fullKey, value);
42
+ }
43
+ });
44
+
45
+ return formData;
46
+ };
47
+
48
+ export type Body = BodyInit | JsonValueWithFile;
49
+
50
+ export const withJsonBody =
51
+ <Body extends JsonValue>(fixedBody: Body) =>
52
+ <Response, PromiseType extends AbortablePromise<Response> | Promise<Response>>(
53
+ req: Request<Response, PromiseType>,
54
+ ) =>
55
+ (options: Omit<RequestOptionsWithoutUrl, 'body'> = {}) =>
56
+ req({
57
+ ...options,
58
+ body: JSON.stringify(fixedBody),
59
+ headers: {
60
+ ...options.headers,
61
+ 'content-type': 'application/json',
62
+ },
63
+ });
64
+
65
+ export const withFormDataBody =
66
+ <Body extends JsonObjectWithFile>(fixedBody: Body) =>
67
+ <Response, PromiseType extends AbortablePromise<Response> | Promise<Response>>(
68
+ req: Request<Response, PromiseType>,
69
+ ) =>
70
+ (options: Omit<RequestOptionsWithoutUrl, 'body'> = {}) =>
71
+ req({
72
+ ...options,
73
+ body: jsonToFormData(fixedBody),
74
+ });
@@ -0,0 +1,11 @@
1
+ export enum HttpMethod {
2
+ CONNECT = 'CONNECT',
3
+ DELETE = 'DELETE',
4
+ GET = 'GET',
5
+ HEAD = 'HEAD',
6
+ OPTIONS = 'OPTIONS',
7
+ PATCH = 'PATCH',
8
+ POST = 'POST',
9
+ PUT = 'PUT',
10
+ TRACE = 'TRACE',
11
+ }
@@ -0,0 +1,75 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { get, patch, post, put } from '../index.js';
4
+ import { HttpMethod } from './httpMethod.js';
5
+ import { QueryParams, withQueryParams } from './queryParams.js';
6
+ import { request } from './request.js';
7
+
8
+ vi.mock('./request.js', () => ({
9
+ request: vi.fn(),
10
+ }));
11
+
12
+ const requestMock = vi.mocked(request);
13
+
14
+ const getRequest = get('/api/data/get');
15
+ const postRequest = post('/api/data/post');
16
+ const putRequest = put('/api/data/put');
17
+ const patchRequest = patch('/api/data/patch');
18
+
19
+ const compareSearchParams = (reference: URLSearchParams, value: URLSearchParams) => {
20
+ const referenceArray = Array.from(reference.entries());
21
+ const valueArray = Array.from(value.entries());
22
+ expect(referenceArray.length).toEqual(valueArray.length);
23
+ expect(valueArray).toEqual(expect.arrayContaining(referenceArray));
24
+ };
25
+
26
+ afterEach(() => {
27
+ vi.clearAllMocks();
28
+ });
29
+
30
+ describe('withQueryParams', () => {
31
+ it('should add single query parameters to the request URL', async () => {
32
+ const query: QueryParams = { baz: 42, foo: 'bar' };
33
+ await withQueryParams(query)(getRequest)();
34
+ const requestSearchParams = requestMock.mock.calls[0][1].searchParams!;
35
+
36
+ compareSearchParams(requestSearchParams, new URLSearchParams('foo=bar&baz=42'));
37
+ });
38
+
39
+ it('should handle array values in query parameters', async () => {
40
+ const query: QueryParams = { foo: ['bar', 'baz'] };
41
+ await withQueryParams(query)(postRequest)();
42
+ const requestSearchParams = requestMock.mock.calls[0][1].searchParams!;
43
+
44
+ compareSearchParams(requestSearchParams, new URLSearchParams('foo=bar&foo=baz'));
45
+ });
46
+
47
+ it('should ignore null and undefined values in query parameters', async () => {
48
+ const query: QueryParams = { baz: null, foo: 'bar', qux: undefined };
49
+ await withQueryParams(query)(putRequest)();
50
+ const requestSearchParams = requestMock.mock.calls[0][1].searchParams!;
51
+
52
+ compareSearchParams(requestSearchParams, new URLSearchParams('foo=bar'));
53
+ });
54
+
55
+ it('should handle boolean values in query parameters', async () => {
56
+ const query: QueryParams = { bar: false, foo: true };
57
+ await withQueryParams(query)(patchRequest)();
58
+ const requestSearchParams = requestMock.mock.calls[0][1].searchParams!;
59
+
60
+ compareSearchParams(requestSearchParams, new URLSearchParams('foo=true&bar=false'));
61
+ });
62
+
63
+ it('should merge query parameters with existing options', async () => {
64
+ const query: QueryParams = { foo: 'bar' };
65
+ await withQueryParams(query)(getRequest)({ headers: { Authorization: 'Bearer token' } });
66
+ const requestSearchParams = requestMock.mock.calls[0][1].searchParams!;
67
+
68
+ compareSearchParams(requestSearchParams, new URLSearchParams('foo=bar'));
69
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.GET, {
70
+ headers: { Authorization: 'Bearer token' },
71
+ searchParams: expect.any(URLSearchParams) as URLSearchParams,
72
+ url: '/api/data/get',
73
+ });
74
+ });
75
+ });
@@ -0,0 +1,31 @@
1
+ import { AbortablePromise } from './abortableRequest.js';
2
+ import { Request } from './request.js';
3
+
4
+ export type QueryParams = Record<string, QueryParamValue | QueryParamValue[]>;
5
+ type QueryParamValue = boolean | null | number | string | undefined;
6
+
7
+ export const queryParamsToURLSearchParams = (query: QueryParams): URLSearchParams => {
8
+ const params = new URLSearchParams();
9
+
10
+ Object.entries(query).forEach(([key, value]) => {
11
+ if (Array.isArray(value)) {
12
+ value.forEach((v) => params.append(key, String(v)));
13
+ } else if (value !== null && value !== undefined) {
14
+ params.append(key, String(value));
15
+ }
16
+ });
17
+
18
+ return params;
19
+ };
20
+
21
+ export const withQueryParams =
22
+ <Query extends QueryParams>(query: Query) =>
23
+ <Response, PromiseType extends AbortablePromise<Response> | Promise<Response>>(
24
+ req: Request<Response, PromiseType>,
25
+ ): Request<Response, PromiseType> =>
26
+ (options = {}) => {
27
+ return req({
28
+ ...options,
29
+ searchParams: queryParamsToURLSearchParams(query),
30
+ });
31
+ };
@@ -0,0 +1,180 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import {
4
+ MiddlewareNext,
5
+ PostResponseMiddlewareContext,
6
+ PreRequestMiddlewareContext,
7
+ usePostResponse,
8
+ usePreRequest,
9
+ } from '../middleware/index.js';
10
+ import { HttpError } from './HttpError.js';
11
+ import { HttpMethod } from './httpMethod.js';
12
+ import { request, RequestOptions } from './request.js';
13
+
14
+ describe('request', () => {
15
+ const mockFetch = vi.fn();
16
+ globalThis.fetch = mockFetch;
17
+
18
+ afterEach(() => {
19
+ mockFetch.mockClear();
20
+ });
21
+
22
+ const mockedResponseFactory = <T extends Record<string, unknown>>(data: T) => ({
23
+ body: JSON.stringify(data),
24
+ json: () => Promise.resolve(data),
25
+ ok: true,
26
+ });
27
+
28
+ it('should make a successful GET request and return JSON response', async () => {
29
+ const mockResponseData = { message: 'Success' };
30
+ mockFetch.mockResolvedValueOnce(mockedResponseFactory(mockResponseData));
31
+
32
+ const options: RequestOptions = {
33
+ method: HttpMethod.GET,
34
+ url: '/api/data',
35
+ };
36
+
37
+ const result = await request<typeof mockResponseData>(HttpMethod.GET, options);
38
+
39
+ expect(mockFetch).toHaveBeenCalledOnce();
40
+ expect(mockFetch).toHaveBeenCalledWith(`${window.location.origin}/api/data`, {
41
+ headers: undefined,
42
+ method: HttpMethod.GET,
43
+ });
44
+ expect(result).toEqual(mockResponseData);
45
+ });
46
+
47
+ it('should append query parameters to the URL', async () => {
48
+ const mockResponseData = { message: 'Success with query params' };
49
+ mockFetch.mockResolvedValueOnce(mockedResponseFactory(mockResponseData));
50
+
51
+ const options: RequestOptions = {
52
+ method: HttpMethod.GET,
53
+ searchParams: new URLSearchParams({ page: '1', search: 'test' }),
54
+ url: '/api/data',
55
+ };
56
+
57
+ const result = await request<typeof mockResponseData>(HttpMethod.GET, options);
58
+
59
+ expect(mockFetch).toHaveBeenCalledOnce();
60
+ expect(mockFetch).toHaveBeenCalledWith(
61
+ `${window.location.origin}/api/data?page=1&search=test`,
62
+ {
63
+ headers: undefined,
64
+ method: HttpMethod.GET,
65
+ },
66
+ );
67
+ expect(result).toEqual(mockResponseData);
68
+ });
69
+
70
+ it('should throw an error if response is not ok', async () => {
71
+ mockFetch.mockResolvedValueOnce({
72
+ ok: false,
73
+ status: 404,
74
+ statusText: 'Not Found',
75
+ });
76
+
77
+ const options: RequestOptions = {
78
+ method: HttpMethod.GET,
79
+ url: '/api/notfound',
80
+ };
81
+
82
+ await expect(request(HttpMethod.GET, options)).rejects.toThrow(new HttpError(404, 'Not Found'));
83
+ expect(mockFetch).toHaveBeenCalledOnce();
84
+ expect(mockFetch).toHaveBeenCalledWith(`${window.location.origin}/api/notfound`, {
85
+ headers: undefined,
86
+ method: HttpMethod.GET,
87
+ });
88
+ });
89
+
90
+ it('should include custom headers in the request', async () => {
91
+ const mockResponseData = { message: 'Success with headers' };
92
+ mockFetch.mockResolvedValueOnce(mockedResponseFactory(mockResponseData));
93
+
94
+ const options: RequestOptions = {
95
+ headers: {
96
+ Authorization: 'Bearer test-token',
97
+ 'Content-Type': 'application/json',
98
+ },
99
+ method: HttpMethod.GET,
100
+ url: '/api/headers',
101
+ };
102
+
103
+ const result = await request<typeof mockResponseData>(HttpMethod.GET, options);
104
+
105
+ expect(mockFetch).toHaveBeenCalledOnce();
106
+ expect(mockFetch).toHaveBeenCalledWith(`${window.location.origin}/api/headers`, {
107
+ headers: {
108
+ Authorization: 'Bearer test-token',
109
+ 'Content-Type': 'application/json',
110
+ },
111
+ method: HttpMethod.GET,
112
+ });
113
+ expect(result).toEqual(mockResponseData);
114
+ });
115
+
116
+ it('should send JSON body in POST request', async () => {
117
+ const mockResponseData = { message: 'Success with JSON body' };
118
+ mockFetch.mockResolvedValueOnce(mockedResponseFactory(mockResponseData));
119
+
120
+ const options: RequestOptions = {
121
+ body: JSON.stringify({ key: 'value' }),
122
+ headers: {
123
+ 'Content-Type': 'application/json',
124
+ },
125
+ method: HttpMethod.POST,
126
+ url: '/api/post',
127
+ };
128
+
129
+ const result = await request<typeof mockResponseData>(HttpMethod.POST, options);
130
+
131
+ expect(mockFetch).toHaveBeenCalledOnce();
132
+ expect(mockFetch).toHaveBeenCalledWith(`${window.location.origin}/api/post`, {
133
+ body: JSON.stringify({ key: 'value' }),
134
+ headers: {
135
+ 'Content-Type': 'application/json',
136
+ },
137
+ method: HttpMethod.POST,
138
+ });
139
+ expect(result).toEqual(mockResponseData);
140
+ });
141
+
142
+ it('should call middleware before and after the request', async () => {
143
+ const mockResponseData = { message: 'Middleware test' };
144
+ mockFetch.mockResolvedValueOnce({
145
+ ...mockedResponseFactory({ ...mockResponseData }),
146
+ status: 200,
147
+ });
148
+
149
+ const options: RequestOptions = {
150
+ method: HttpMethod.GET,
151
+ url: '/api/middleware',
152
+ headers: {
153
+ 'X-Test-Header': 'TestValue',
154
+ },
155
+ };
156
+
157
+ const preRequestMiddleware = vi.fn(
158
+ async (ctx: PreRequestMiddlewareContext, next: MiddlewareNext) => {
159
+ expect(ctx.request.method).toBe(HttpMethod.GET);
160
+ expect((ctx.request.headers! as Record<string, string>)['X-Test-Header']).toBe('TestValue');
161
+ await next();
162
+ },
163
+ );
164
+ const postResponseMiddleware = vi.fn(
165
+ async (ctx: PostResponseMiddlewareContext, next: MiddlewareNext) => {
166
+ expect(ctx.response.ok).toBe(true);
167
+ expect(ctx.response.status).toBe(200);
168
+ expect(ctx.response.body).toBe(JSON.stringify(mockResponseData));
169
+ await next();
170
+ },
171
+ );
172
+ usePreRequest(preRequestMiddleware);
173
+ usePostResponse(postResponseMiddleware);
174
+
175
+ await request<typeof mockResponseData>(HttpMethod.GET, options);
176
+
177
+ expect(preRequestMiddleware).toHaveBeenCalledOnce();
178
+ expect(postResponseMiddleware).toHaveBeenCalledOnce();
179
+ });
180
+ });
@@ -0,0 +1,57 @@
1
+ import { runPostResponse, runPreRequest } from '../middleware/runner.js';
2
+ import { AbortablePromise } from './abortableRequest.js';
3
+ import { BaseURL, getDefaultBaseUrl } from './baseUrl.js';
4
+ import { HttpError } from './HttpError.js';
5
+ import { HttpMethod } from './httpMethod.js';
6
+
7
+ type PathOnly = `../${string}` | `./${string}` | `/${string}`;
8
+
9
+ export type RequestURL = BaseURL | PathOnly;
10
+
11
+ export type Request<
12
+ Response,
13
+ PromiseType extends AbortablePromise<Response> | Promise<Response>,
14
+ > = (options?: RequestOptionsWithoutUrl) => PromiseType;
15
+
16
+ export type RequestOptions = RequestInit & {
17
+ searchParams?: URLSearchParams;
18
+ url: RequestURL;
19
+ baseUrl?: BaseURL;
20
+ };
21
+
22
+ export type RequestOptionsWithoutUrl = Omit<RequestOptions, 'url'>;
23
+
24
+ export const request = async <Response>(
25
+ method: HttpMethod,
26
+ options: RequestOptions,
27
+ ): Promise<Response> => {
28
+ const fetchOptions = {
29
+ method,
30
+ ...options,
31
+ };
32
+ const middlewareContext = { request: fetchOptions };
33
+ await runPreRequest(middlewareContext);
34
+
35
+ const { searchParams, url, baseUrl, ...requestOptions } = fetchOptions;
36
+
37
+ const fullUrl = new URL(url, baseUrl || getDefaultBaseUrl());
38
+ if (searchParams) {
39
+ searchParams.forEach((value, key) => {
40
+ fullUrl.searchParams.append(key, value);
41
+ });
42
+ }
43
+
44
+ const response = await fetch(fullUrl.toString(), requestOptions);
45
+
46
+ await runPostResponse({ ...middlewareContext, response });
47
+
48
+ if (!response.ok) {
49
+ throw new HttpError(response.status, response.statusText);
50
+ }
51
+
52
+ if (response.status === 204 || !response.body) {
53
+ return undefined as Response;
54
+ }
55
+
56
+ return (await response.json()) as Response;
57
+ };
@@ -0,0 +1,66 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { get } from '../index.js';
4
+ import { HttpError } from './HttpError.js';
5
+ import { HttpMethod } from './httpMethod.js';
6
+ import { request, RequestOptionsWithoutUrl } from './request.js';
7
+ import { withNotFoundAsNull } from './status.js';
8
+
9
+ vi.mock('./request.js', () => ({
10
+ request: vi.fn(),
11
+ }));
12
+
13
+ const requestMock = vi.mocked(request);
14
+
15
+ const getRequest = get('/api/data/get');
16
+
17
+ afterEach(() => {
18
+ vi.clearAllMocks();
19
+ });
20
+
21
+ describe('withNotFoundAsNull', () => {
22
+ it('should return the response when the request succeeds', async () => {
23
+ const mockResponse = { data: 'valid response' };
24
+ requestMock.mockResolvedValueOnce(mockResponse);
25
+
26
+ const fetchData = withNotFoundAsNull(getRequest);
27
+ const result = await fetchData();
28
+
29
+ expect(result).toEqual(mockResponse);
30
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.GET, { url: '/api/data/get' });
31
+ });
32
+
33
+ it('should return null when the request fails with 404', async () => {
34
+ requestMock.mockRejectedValueOnce(new HttpError(404, 'Not Found'));
35
+
36
+ const fetchData = withNotFoundAsNull(getRequest);
37
+ const result = await fetchData();
38
+
39
+ expect(result).toBeNull();
40
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.GET, { url: '/api/data/get' });
41
+ });
42
+
43
+ it('should throw an error when the request fails with an error other than 404', async () => {
44
+ requestMock.mockRejectedValueOnce(new HttpError(500, 'Internal Server Error'));
45
+
46
+ const fetchData = withNotFoundAsNull(getRequest);
47
+
48
+ await expect(fetchData()).rejects.toThrow(new HttpError(500, 'Internal Server Error'));
49
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.GET, { url: '/api/data/get' });
50
+ });
51
+
52
+ it('should forward request options when calling the request function', async () => {
53
+ const mockResponse = { data: 'valid response' };
54
+ requestMock.mockResolvedValueOnce(mockResponse);
55
+
56
+ const fetchData = withNotFoundAsNull(getRequest);
57
+ const options: RequestOptionsWithoutUrl = { headers: { Authorization: 'Bearer token' } };
58
+ const result = await fetchData(options);
59
+
60
+ expect(result).toEqual(mockResponse);
61
+ expect(requestMock).toHaveBeenCalledWith(HttpMethod.GET, {
62
+ url: '/api/data/get',
63
+ headers: { Authorization: 'Bearer token' },
64
+ });
65
+ });
66
+ });
@@ -0,0 +1,15 @@
1
+ import { HttpError } from './HttpError.js';
2
+ import { RequestOptionsWithoutUrl } from './request.js';
3
+
4
+ export const withNotFoundAsNull =
5
+ <Response>(requestFn: (options?: RequestOptionsWithoutUrl) => Promise<Response>) =>
6
+ async (options?: RequestOptionsWithoutUrl): Promise<null | Response> => {
7
+ try {
8
+ return await requestFn(options);
9
+ } catch (error) {
10
+ if (error instanceof HttpError && error.status == 404) {
11
+ return null;
12
+ }
13
+ throw error;
14
+ }
15
+ };
@@ -0,0 +1,10 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.json",
4
+ "compilerOptions": {
5
+ "noEmit": false,
6
+ "outDir": "./dist",
7
+ "rootDir": "./src"
8
+ },
9
+ "exclude": ["node_modules", "dist", "src/**/*.spec.ts"]
10
+ }