Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize API route paths #816

Merged
merged 2 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Unreleased

- Allow trailing slash in API routes.

## 0.40.1 (Release Jul 27, 2023)

- Use Node.js 18.x Lambda runtime to ensure AWS SDK 3.0 is available
- Use Node.js 18.x Lambda runtime to ensure AWS SDK 3.0 is available.

## 0.40.0 (Release Jul 27, 2023)

Expand Down
28 changes: 16 additions & 12 deletions aws/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,28 @@ export class API implements cloud.API {
this.customDomains = [];
}

public static(path: string, localPath: string, options?: cloud.ServeStaticOptions) {
private cleanPath(path: string): string {
if (path.endsWith("/")) {
path = path.slice(0, -1);
}
if (!path.startsWith("/")) {
path = "/" + path;
}
return path;
}

public static(path: string, localPath: string, options?: cloud.ServeStaticOptions) {
path = this.cleanPath(path);
this.staticRoutes.push({ path, localPath, options: options || {} });
}

public proxy(path: string, target: string | pulumi.Output<cloud.Endpoint>) {
if (!path.startsWith("/")) {
path = "/" + path;
}
path = this.cleanPath(path);
this.proxyRoutes.push({ path, target });
}

public route(method: string, path: string, ...handlers: cloud.RouteHandler[]) {
if (!path.startsWith("/")) {
path = "/" + path;
}
path = this.cleanPath(path);
this.routes.push({ method: method, path: path, handlers: handlers });
}

Expand Down Expand Up @@ -174,13 +178,13 @@ export class HttpDeployment extends pulumi.ComponentResource implements cloud.Ht
};
}

const awsDomain = new aws.apigateway.DomainName(apiNameAndHash, domainArgs, {parent});
const awsDomain = new aws.apigateway.DomainName(apiNameAndHash, domainArgs, { parent });

const basePathMapping = new aws.apigateway.BasePathMapping(apiNameAndHash, {
restApi: api,
stageName: stageName,
domainName: awsDomain.domainName,
}, {parent});
}, { parent });

awsDomains.push(awsDomain);
}
Expand All @@ -191,7 +195,7 @@ export class HttpDeployment extends pulumi.ComponentResource implements cloud.Ht
constructor(name: string, staticRoutes: StaticRoute[], proxyRoutes: ProxyRoute[],
routes: Route[], customDomains: Domain[], opts?: pulumi.ResourceOptions) {

super("cloud:http:API", name, { }, opts);
super("cloud:http:API", name, {}, opts);

this.staticRoutes = staticRoutes;
this.proxyRoutes = proxyRoutes;
Expand Down Expand Up @@ -334,10 +338,10 @@ function convertHandlers(name: string, route: Route, opts: pulumi.ResourceOption

const stageName = "stage";
function apiGatewayToRequestResponse(
ev: awsx.apigateway.Request, body: Buffer, cb: (err: any, result: awsx.apigateway.Response) => void): [cloud.Request, cloud.Response] {
ev: awsx.apigateway.Request, body: Buffer, cb: (err: any, result: awsx.apigateway.Response) => void): [cloud.Request, cloud.Response] {
const response = {
statusCode: 200,
headers: <{[header: string]: string}>{},
headers: <{ [header: string]: string }>{},
body: Buffer.from([]),
};
const headers: { [name: string]: string; } = {};
Expand Down
Loading