Skip to content

Commit 1fb360f

Browse files
committed
fix: 🐛 made buildRouteArn a static method of AuthorizerResponse class
1 parent 25e504a commit 1fb360f

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/authorizerResponse.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,6 @@ export const DENY_ALL_RESPONSE: AwsPolicy = {
1515
},
1616
};
1717

18-
export function buildRouteArn(
19-
region: string,
20-
awsAccountId: string,
21-
apiId: string,
22-
stage: string,
23-
method: string,
24-
path: string
25-
): string {
26-
// Clean all starting / from path
27-
while (path.startsWith("/")) {
28-
path = path.substring(1, path.length);
29-
}
30-
31-
// Match regex with path variables and replace them with "*"
32-
const regex = /{[a-zA-Z_$]+[a-zA-Z0-9_$]*}/g;
33-
path = path.replace(regex, "*");
34-
return `arn:aws:execute-api:${region}:${awsAccountId}:${apiId}/${stage}/${method}/${path}`;
35-
}
36-
3718
export type ApiGatewayArnParts = {
3819
region: string;
3920
awsAccountId: string;
@@ -171,6 +152,25 @@ export class AuthorizerResponse {
171152
};
172153
}
173154

155+
public static buildRouteArn(
156+
region: string,
157+
awsAccountId: string,
158+
apiId: string,
159+
stage: string,
160+
method: string,
161+
path: string
162+
): string {
163+
// Clean all starting / from path
164+
while (path.startsWith("/")) {
165+
path = path.substring(1, path.length);
166+
}
167+
168+
// Match regex with path variables and replace them with "*"
169+
const regex = /{[a-zA-Z_$]+[a-zA-Z0-9_$]*}/g;
170+
path = path.replace(regex, "*");
171+
return `arn:aws:execute-api:${region}:${awsAccountId}:${apiId}/${stage}/${method}/${path}`;
172+
}
173+
174174
public static fromMethodArn(
175175
principalId: string,
176176
arn: string,
@@ -229,7 +229,7 @@ export class AuthorizerResponse {
229229
}
230230
if (effect === "Allow") {
231231
this.allowedRoutes.push({
232-
resourceArn: buildRouteArn(
232+
resourceArn: AuthorizerResponse.buildRouteArn(
233233
this.region,
234234
this.awsAccountId,
235235
this.apiId,
@@ -241,7 +241,7 @@ export class AuthorizerResponse {
241241
});
242242
} else {
243243
this.deniedRoutes.push({
244-
resourceArn: buildRouteArn(
244+
resourceArn: AuthorizerResponse.buildRouteArn(
245245
this.region,
246246
this.awsAccountId,
247247
this.apiId,

test/route-arn.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "mocha";
2-
import { buildRouteArn } from "../src";
2+
import { AuthorizerResponse } from "../src";
33
import { expect } from "chai";
44

55
const region = "us-west-2";
@@ -12,7 +12,7 @@ describe("Route ARN Path Regex Test Suite", () => {
1212
it("Will replace path variables with '*'", () => {
1313
const path = "/variable/{id}";
1414

15-
const result = buildRouteArn(
15+
const result = AuthorizerResponse.buildRouteArn(
1616
region,
1717
awsAccountId,
1818
apiId,
@@ -27,7 +27,7 @@ describe("Route ARN Path Regex Test Suite", () => {
2727
it("Will replace multiple path variables with '*'", () => {
2828
const path = "/variable/{id}/hello/{name}";
2929

30-
const result = buildRouteArn(
30+
const result = AuthorizerResponse.buildRouteArn(
3131
region,
3232
awsAccountId,
3333
apiId,

0 commit comments

Comments
 (0)