@squiz/dx-common-lib 1.72.3 → 1.72.5
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.
- package/.npm/_logs/2026-09-08T06_40_25_662Z-debug-0.log +19 -0
- package/CHANGELOG.md +12 -0
- package/lib/cloudflare-api/CloudflareApiService.d.ts +16 -0
- package/lib/cloudflare-api/CloudflareApiService.js +45 -0
- package/lib/cloudflare-api/CloudflareApiService.js.map +1 -0
- package/lib/cloudflare-api/CloudflareApiService.spec.d.ts +1 -0
- package/lib/cloudflare-api/CloudflareApiService.spec.js +50 -0
- package/lib/cloudflare-api/CloudflareApiService.spec.js.map +1 -0
- package/lib/cloudflare-service-secret-key-service/getCloudflareServiceSecretService.d.ts +6 -0
- package/lib/cloudflare-service-secret-key-service/getCloudflareServiceSecretService.js +8 -0
- package/lib/cloudflare-service-secret-key-service/getCloudflareServiceSecretService.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -1
- package/lib/outbound-http/createServiceUserAgent.d.ts +6 -0
- package/lib/outbound-http/createServiceUserAgent.js +13 -0
- package/lib/outbound-http/createServiceUserAgent.js.map +1 -0
- package/lib/outbound-http/createServiceUserAgent.spec.d.ts +1 -0
- package/lib/outbound-http/createServiceUserAgent.spec.js +12 -0
- package/lib/outbound-http/createServiceUserAgent.spec.js.map +1 -0
- package/lib/outbound-http/index.d.ts +2 -0
- package/lib/outbound-http/index.js +8 -0
- package/lib/outbound-http/index.js.map +1 -0
- package/lib/outbound-http/outboundHttp.d.ts +2 -0
- package/lib/outbound-http/outboundHttp.js +23 -0
- package/lib/outbound-http/outboundHttp.js.map +1 -0
- package/lib/outbound-http/outboundHttp.spec.d.ts +1 -0
- package/lib/outbound-http/outboundHttp.spec.js +59 -0
- package/lib/outbound-http/outboundHttp.spec.js.map +1 -0
- package/lib/stream/EventStreamHandler.d.ts +3 -2
- package/lib/stream/EventStreamHandler.js.map +1 -1
- package/package.json +3 -3
- package/src/cloudflare-api/CloudflareApiService.spec.ts +66 -0
- package/src/cloudflare-api/CloudflareApiService.ts +61 -0
- package/src/cloudflare-service-secret-key-service/getCloudflareServiceSecretService.ts +11 -0
- package/src/index.ts +3 -0
- package/src/outbound-http/createServiceUserAgent.spec.ts +11 -0
- package/src/outbound-http/createServiceUserAgent.ts +9 -0
- package/src/outbound-http/index.ts +2 -0
- package/src/outbound-http/outboundHttp.spec.ts +69 -0
- package/src/outbound-http/outboundHttp.ts +26 -0
- package/src/stream/EventStreamHandler.spec.ts +1 -1
- package/src/stream/EventStreamHandler.ts +4 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createServiceUserAgent } from './createServiceUserAgent';
|
|
2
|
+
|
|
3
|
+
describe('createServiceUserAgent', () => {
|
|
4
|
+
it('returns Squiz/<name> for a scoped package name', () => {
|
|
5
|
+
expect(createServiceUserAgent('@squiz/content-api')).toBe('Squiz/content-api');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('returns Squiz/<name> for an unscoped service name', () => {
|
|
9
|
+
expect(createServiceUserAgent('content-api')).toBe('Squiz/content-api');
|
|
10
|
+
});
|
|
11
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds a User-Agent string for outbound service-to-service HTTP requests.
|
|
3
|
+
*
|
|
4
|
+
* @param serviceName Service identifier, e.g. `content-api` or `@squiz/content-api`
|
|
5
|
+
*/
|
|
6
|
+
export function createServiceUserAgent(serviceName: string): string {
|
|
7
|
+
const name = serviceName.replace(/^@squiz\//, '');
|
|
8
|
+
return `Squiz/${name}`;
|
|
9
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { outboundFetch } from './outboundHttp';
|
|
2
|
+
|
|
3
|
+
describe('outboundFetch', () => {
|
|
4
|
+
const userAgent = 'Squiz/test-service';
|
|
5
|
+
const originalFetch = global.fetch;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
global.fetch = jest.fn().mockResolvedValue(new Response());
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
global.fetch = originalFetch;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('adds User-Agent to string URL requests with init headers', async () => {
|
|
16
|
+
await outboundFetch(userAgent, 'https://example.com/resource', {
|
|
17
|
+
headers: { 'x-api-key': 'secret' },
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const init = (global.fetch as jest.Mock).mock.calls[0][1] as RequestInit;
|
|
21
|
+
const headers = init.headers as Headers;
|
|
22
|
+
expect(headers.get('x-api-key')).toBe('secret');
|
|
23
|
+
expect(headers.get('User-Agent')).toBe(userAgent);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('preserves headers from a Request input and adds User-Agent', async () => {
|
|
27
|
+
const request = new Request('https://example.com/resource', {
|
|
28
|
+
method: 'GET',
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: 'Bearer token',
|
|
31
|
+
'x-dxp-tenant': 'tenant-1',
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await outboundFetch(userAgent, request);
|
|
36
|
+
|
|
37
|
+
const init = (global.fetch as jest.Mock).mock.calls[0][1] as RequestInit;
|
|
38
|
+
const headers = init.headers as Headers;
|
|
39
|
+
expect(headers.get('Authorization')).toBe('Bearer token');
|
|
40
|
+
expect(headers.get('x-dxp-tenant')).toBe('tenant-1');
|
|
41
|
+
expect(headers.get('User-Agent')).toBe(userAgent);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('merges init headers on top of Request headers without dropping existing values', async () => {
|
|
45
|
+
const request = new Request('https://example.com/resource', {
|
|
46
|
+
headers: { 'x-api-key': 'from-request' },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
await outboundFetch(userAgent, request, {
|
|
50
|
+
headers: { 'x-dxp-session': 'session-token' },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const init = (global.fetch as jest.Mock).mock.calls[0][1] as RequestInit;
|
|
54
|
+
const headers = init.headers as Headers;
|
|
55
|
+
expect(headers.get('x-api-key')).toBe('from-request');
|
|
56
|
+
expect(headers.get('x-dxp-session')).toBe('session-token');
|
|
57
|
+
expect(headers.get('User-Agent')).toBe(userAgent);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('Overrides an explicit User-Agent header', async () => {
|
|
61
|
+
await outboundFetch(userAgent, 'https://example.com/resource', {
|
|
62
|
+
headers: { 'User-Agent': 'custom-agent' },
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const init = (global.fetch as jest.Mock).mock.calls[0][1] as RequestInit;
|
|
66
|
+
const headers = init.headers as Headers;
|
|
67
|
+
expect(headers.get('User-Agent')).toBe(userAgent);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type OutboundFetchInput = string | URL | Request;
|
|
2
|
+
|
|
3
|
+
function buildOutboundHeaders(userAgent: string, input: OutboundFetchInput, init?: RequestInit): Headers {
|
|
4
|
+
const headers = new Headers();
|
|
5
|
+
|
|
6
|
+
if (input instanceof Request) {
|
|
7
|
+
input.headers.forEach((value, key) => {
|
|
8
|
+
headers.set(key, value);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (init?.headers) {
|
|
13
|
+
new Headers(init.headers).forEach((value, key) => {
|
|
14
|
+
headers.set(key, value);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
headers.set('User-Agent', userAgent);
|
|
19
|
+
|
|
20
|
+
return headers;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function outboundFetch(userAgent: string, input: OutboundFetchInput, init?: RequestInit): Promise<Response> {
|
|
24
|
+
const headers = buildOutboundHeaders(userAgent, input, init);
|
|
25
|
+
return fetch(input, { ...init, headers });
|
|
26
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Copyright Squiz Australia Pty Ltd. All Rights Reserved.
|
|
4
4
|
*/
|
|
5
5
|
import { createEventStreamHandler, EventMapper } from './EventStreamHandler';
|
|
6
|
-
import { DynamoDBStreamEvent, DynamoDBRecord, Context } from 'aws-lambda';
|
|
6
|
+
import type { DynamoDBStreamEvent, DynamoDBRecord, Context } from 'aws-lambda';
|
|
7
7
|
|
|
8
8
|
interface MockLogger {
|
|
9
9
|
info: jest.Mock;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @license
|
|
3
3
|
* Copyright Squiz Australia Pty Ltd. All Rights Reserved.
|
|
4
4
|
*/
|
|
5
|
-
import { DynamoDBStreamEvent, DynamoDBRecord, Context, DynamoDBBatchResponse } from 'aws-lambda';
|
|
5
|
+
import type { DynamoDBStreamEvent, DynamoDBRecord, Context, DynamoDBBatchResponse } from 'aws-lambda';
|
|
6
6
|
import { Logger } from '@squiz/dx-logger-lib';
|
|
7
7
|
import { EventBusService } from '../events/EventBusService';
|
|
8
8
|
|
|
@@ -16,6 +16,8 @@ export type EventMapper = (record: DynamoDBRecord) => Promise<EventPayload[]>;
|
|
|
16
16
|
/** Resolves tenant id for publishing; use `record` when tenant is carried on the item rather than derivable from the table ARN. */
|
|
17
17
|
export type ExtractTenantId = (arn: string, tableServiceIdentifier: string, record: DynamoDBRecord) => string;
|
|
18
18
|
|
|
19
|
+
export type EventStreamHandler = (event: DynamoDBStreamEvent, context: Context) => Promise<DynamoDBBatchResponse>;
|
|
20
|
+
|
|
19
21
|
interface MappedRecord {
|
|
20
22
|
recordIndex: number;
|
|
21
23
|
record: DynamoDBRecord;
|
|
@@ -46,7 +48,7 @@ interface EventStreamHandlerConfig<T extends string> {
|
|
|
46
48
|
* - Batch event publishing with retry tracking
|
|
47
49
|
* - Failure reporting for Lambda retry mechanism
|
|
48
50
|
*/
|
|
49
|
-
export function createEventStreamHandler<T extends string>(config: EventStreamHandlerConfig<T>) {
|
|
51
|
+
export function createEventStreamHandler<T extends string>(config: EventStreamHandlerConfig<T>): EventStreamHandler {
|
|
50
52
|
const {
|
|
51
53
|
logger,
|
|
52
54
|
eventBusService,
|