Skip to content

Commit 227fe5b

Browse files
authored
🤖 Merge PR DefinitelyTyped#72713 Update @types/aws-cloudfront-function to include kvs and origin helper methods by @46ki75
1 parent 4771e16 commit 227fe5b

File tree

4 files changed

+244
-2
lines changed

4 files changed

+244
-2
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"rules": {
3-
"@definitelytyped/no-type-only-packages": "off"
3+
"@definitelytyped/no-type-only-packages": "off",
4+
"@definitelytyped/no-single-declare-module": "off"
45
}
56
}

‎types/aws-cloudfront-function/aws-cloudfront-function-tests.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,67 @@ function handler2(event: AWSCloudFrontFunction.Event): AWSCloudFrontFunction.Req
111111

112112
return response(locale, request.uri);
113113
}
114+
115+
import cf from "cloudfront";
116+
117+
const kvsHandle = cf.kvs("example-kvs-id");
118+
119+
async function handler3(
120+
event: AWSCloudFrontFunction.Event,
121+
): Promise<AWSCloudFrontFunction.Request | AWSCloudFrontFunction.Response> {
122+
const _value1 = await kvsHandle.get("key");
123+
const _value2 = await kvsHandle.get("key", { format: "string" });
124+
const _value3 = await kvsHandle.get("key", { format: "bytes" });
125+
const _value4 = await kvsHandle.get("key", { format: "json" });
126+
127+
const _exists = await kvsHandle.exists("key");
128+
129+
const _meta = await kvsHandle.meta();
130+
131+
return event.request;
132+
}
133+
134+
function testUpdateRequestOrigin() {
135+
const params: Parameters<typeof cf.updateRequestOrigin>[0] = {
136+
domainName: "example.com",
137+
originPath: "/new-path",
138+
customHeaders: { "x-custom-header": "value" },
139+
connectionAttempts: 3,
140+
originShield: {
141+
enabled: true,
142+
region: "us-east-1",
143+
},
144+
originAccessControlConfig: {
145+
enabled: true,
146+
signingBehavior: "always",
147+
signingProtocol: "sigv4",
148+
originType: "s3",
149+
},
150+
timeouts: {
151+
readTimeout: 30,
152+
keepAliveTimeout: 15,
153+
connectionTimeout: 10,
154+
},
155+
customOriginConfig: {
156+
port: 443,
157+
protocol: "https",
158+
sslProtocols: ["TLSv1.2", "TLSv1.1"],
159+
},
160+
};
161+
cf.updateRequestOrigin(params);
162+
}
163+
164+
function testSelectRequestOriginById() {
165+
const originId: Parameters<typeof cf.selectRequestOriginById>[0] = "example-origin-id";
166+
cf.selectRequestOriginById(originId);
167+
}
168+
169+
function testCreateRequestOriginGroup() {
170+
const params: Parameters<typeof cf.createRequestOriginGroup>[0] = {
171+
originIds: ["origin-1", "origin-2"],
172+
failoverCriteria: {
173+
statusCodes: [500, 502, 503, 504],
174+
},
175+
};
176+
cf.createRequestOriginGroup(params);
177+
}

‎types/aws-cloudfront-function/index.d.ts‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,178 @@ declare namespace AWSCloudFrontFunction {
5353
};
5454
}
5555
}
56+
57+
declare module "cloudfront" {
58+
/**
59+
* Retrieves a reference to a CloudFront Key-Value Store (KVS) by its ID.
60+
* @param kvsId The identifier of the KVS to use.
61+
* @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-custom-methods.html
62+
*/
63+
function kvs(kvsId: string): KVStore;
64+
65+
interface KVStore {
66+
/**
67+
* Retrieve a value from the store.
68+
* @param key Key to retrieve.
69+
* @throws If key does not exist.
70+
*/
71+
get(key: string): Promise<string>;
72+
get(key: string, options: { format: "string" }): Promise<string>;
73+
get(key: string, options: { format: "bytes" }): Promise<Uint8Array>;
74+
get(key: string, options: { format: "json" }): Promise<unknown>;
75+
76+
/**
77+
* Check if the key exists in the store.
78+
* @param key Key to check.
79+
*/
80+
exists(key: string): Promise<boolean>;
81+
82+
/**
83+
* Retrieve metadata about the key-value store.
84+
*/
85+
meta(): Promise<{
86+
creationDateTime: string;
87+
lastUpdatedDateTime: string;
88+
keyCount: number;
89+
}>;
90+
}
91+
92+
interface OriginAccessControlConfig {
93+
enabled: boolean;
94+
signingBehavior: "always" | "never" | "no-override";
95+
signingProtocol: "sigv4";
96+
originType: "s3" | "mediapackagev2" | "mediastore" | "lambda";
97+
}
98+
99+
interface OriginShield {
100+
enabled: boolean;
101+
region: string;
102+
}
103+
104+
interface Timeouts {
105+
/**
106+
* Max time (seconds) to wait for a response or next packet. (1–60)
107+
*/
108+
readTimeout?: number;
109+
110+
/**
111+
* Max time (seconds) to keep the connection alive after response. (1–60)
112+
*/
113+
keepAliveTimeout?: number;
114+
115+
/**
116+
* Max time (seconds) to wait for connection establishment. (1–10)
117+
*/
118+
connectionTimeout?: number;
119+
}
120+
121+
interface CustomOriginConfig {
122+
/**
123+
* Port number of the origin. e.g., 80 or 443
124+
*/
125+
port: number;
126+
127+
/**
128+
* Protocol used to connect. Must be "http" or "https"
129+
*/
130+
protocol: "http" | "https";
131+
132+
/**
133+
* Minimum TLS/SSL version to use for HTTPS connections.
134+
*/
135+
sslProtocols: Array<"SSLv3" | "TLSv1" | "TLSv1.1" | "TLSv1.2">;
136+
}
137+
138+
interface UpdateRequestOriginParams {
139+
/**
140+
* New origin's domain name. Optional if reusing existing origin's domain.
141+
*/
142+
domainName?: string;
143+
144+
/**
145+
* Path prefix to append when forwarding request to origin.
146+
*/
147+
originPath?: string;
148+
149+
/**
150+
* Override or clear custom headers for the origin request.
151+
*/
152+
customHeaders?: Record<string, string>;
153+
154+
/**
155+
* Number of connection attempts (1–3).
156+
*/
157+
connectionAttempts?: number;
158+
159+
/**
160+
* Origin Shield configuration. Enables shield layer if specified.
161+
*/
162+
originShield?: OriginShield;
163+
164+
/**
165+
* Origin Access Control (OAC) configuration.
166+
*/
167+
originAccessControlConfig?: OriginAccessControlConfig;
168+
169+
/**
170+
* Response and connection timeout configurations.
171+
*/
172+
timeouts?: Timeouts;
173+
174+
/**
175+
* Settings for non-S3 origins or S3 with static website hosting.
176+
*/
177+
customOriginConfig?: CustomOriginConfig;
178+
}
179+
180+
/**
181+
* Mutates the current request’s origin.
182+
* You can specify a new origin (e.g., S3 or ALB), change custom headers, enable OAC, or enable Origin Shield.
183+
* Missing fields will inherit values from the assigned origin.
184+
* @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/helper-functions-origin-modification.html#update-request-origin-helper-function
185+
*/
186+
function updateRequestOrigin(params: UpdateRequestOriginParams): void;
187+
188+
/**
189+
* Switches to another origin already defined in the distribution by origin ID.
190+
* This is more efficient than defining a new one via `updateRequestOrigin()`.
191+
* @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/helper-functions-origin-modification.html#select-request-origin-id-helper-function
192+
*/
193+
function selectRequestOriginById(originId: string): void;
194+
195+
interface CreateRequestOriginGroupParams {
196+
/**
197+
* Two origin IDs to form an origin group.
198+
* The first is primary; the second is used for failover.
199+
*/
200+
originIds: [string, string];
201+
202+
/**
203+
* Failover selection strategy: default or media-quality-score.
204+
*/
205+
selectionCriteria?: "default" | "media-quality-score";
206+
207+
/**
208+
* List of status codes that trigger failover to the secondary origin.
209+
*/
210+
failoverCriteria: {
211+
statusCodes: number[];
212+
};
213+
}
214+
215+
/**
216+
* Creates a new origin group for failover logic.
217+
* The origin group can be referenced later via origin ID.
218+
* @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/helper-functions-origin-modification.html#create-request-origin-group-helper-function
219+
*/
220+
function createRequestOriginGroup(params: CreateRequestOriginGroupParams): void;
221+
222+
const cf: {
223+
kvs: typeof kvs;
224+
updateRequestOrigin: typeof updateRequestOrigin;
225+
selectRequestOriginById: typeof selectRequestOriginById;
226+
createRequestOriginGroup: typeof createRequestOriginGroup;
227+
};
228+
229+
export default cf;
230+
}

‎types/aws-cloudfront-function/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"nonNpm": true,
66
"nonNpmDescription": "AWS CloudFront Functions",
77
"projects": [
8-
"https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html"
8+
"https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html",
9+
"https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-custom-methods.html",
10+
"https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/helper-functions-origin-modification.html"
911
],
1012
"devDependencies": {
1113
"@types/aws-cloudfront-function": "workspace:."

0 commit comments

Comments
 (0)