Skip to content

Commit bb1116d

Browse files
committed
feat(type-safe-api): add metadata option to typescript codegen for esm compatible code
Adds an option to the typescript generators to emit esm compatible code (eg imports ending with .js).
1 parent 86f3b72 commit bb1116d

File tree

26 files changed

+4794
-75
lines changed

26 files changed

+4794
-75
lines changed

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-cdk-infrastructure/templates/api.ejs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
###/TSAPI_WRITE_FILE###import { TypeSafeWebsocketApi, TypeSafeWebsocketApiProps, TypeSafeWebsocketApiIntegration } from "@aws/pdk/type-safe-api";
1010
import { Construct } from "constructs";
1111
import { OperationConfig, OperationLookup } from "<%- metadata.runtimePackageName %>";
12+
<%_ if (metadata.esm) { _%>
13+
import * as url from 'url';
14+
<%_ } else { _%>
1215
import * as path from "path";
16+
<%_ } _%>
1317

1418
export type WebSocketApiIntegrations = OperationConfig<TypeSafeWebsocketApiIntegration>;
1519

@@ -27,7 +31,11 @@ export class WebSocketApi extends TypeSafeWebsocketApi {
2731
...props,
2832
integrations: props.integrations as any,
2933
operationLookup: OperationLookup,
34+
<%_ if (metadata.esm) { _%>
35+
specPath: url.fileURLToPath(new URL("<%- metadata.relativeSpecPath %>", import.meta.url)),
36+
<%_ } else { _%>
3037
specPath: path.resolve(__dirname, "<%- metadata.relativeSpecPath %>"),
38+
<%_ } _%>
3139
});
3240
}
3341
}

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-cdk-infrastructure/templates/functions.ejs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { Duration } from "aws-cdk-lib";
1010
import { SnapStartFunction, SnapStartFunctionProps } from "@aws/pdk/type-safe-api";
1111
import { Code, Function, Runtime, Tracing, FunctionProps } from "aws-cdk-lib/aws-lambda";
1212
import * as path from "path";
13+
<%_ if (metadata.esm) { _%>
14+
import * as url from 'url';
15+
<%_ } _%>
1316

1417
<%_ if (vendorExtensions['x-connect-handler']) { _%>
1518
<%_ const language = vendorExtensions['x-connect-handler'].language; _%>
@@ -41,7 +44,7 @@ export class $ConnectFunction extends <% if (isJava) { %>SnapStart<% } %>Functio
4144
<%_ } else if (isJava) { _%>
4245
handler: "<%- metadata['x-handlers-java-package'] %>.$ConnectHandler",
4346
<%_ } _%>
44-
code: Code.fromAsset(path.resolve(__dirname, "..",
47+
code: Code.fromAsset(<%_ if (metadata.esm) { _%>url.fileURLToPath(new URL(path.join("..",<%_ } else { _%>path.resolve(__dirname, "..",<%_ } %>
4548
<%_ if (isTypeScript) { _%>
4649
"<%- metadata['x-handlers-typescript-asset-path'] %>",
4750
"$connect",
@@ -50,7 +53,7 @@ export class $ConnectFunction extends <% if (isJava) { %>SnapStart<% } %>Functio
5053
<%_ } else if (isJava) { _%>
5154
"<%- metadata['x-handlers-java-asset-path'] %>",
5255
<%_ } _%>
53-
)),
56+
)<%_ if (metadata.esm) { _%>, import.meta.url))<%_ } _%>),
5457
tracing: Tracing.ACTIVE,
5558
timeout: Duration.seconds(30),
5659
...props,
@@ -89,7 +92,7 @@ export class $DisconnectFunction extends <% if (isJava) { %>SnapStart<% } %>Func
8992
<%_ } else if (isJava) { _%>
9093
handler: "<%- metadata['x-handlers-java-package'] %>.$DisconnectHandler",
9194
<%_ } _%>
92-
code: Code.fromAsset(path.resolve(__dirname, "..",
95+
code: Code.fromAsset(<%_ if (metadata.esm) { _%>url.fileURLToPath(new URL(path.join("..",<%_ } else { _%>path.resolve(__dirname, "..",<%_ } %>
9396
<%_ if (isTypeScript) { _%>
9497
"<%- metadata['x-handlers-typescript-asset-path'] %>",
9598
"$disconnect",
@@ -98,7 +101,7 @@ export class $DisconnectFunction extends <% if (isJava) { %>SnapStart<% } %>Func
98101
<%_ } else if (isJava) { _%>
99102
"<%- metadata['x-handlers-java-asset-path'] %>",
100103
<%_ } _%>
101-
)),
104+
)<%_ if (metadata.esm) { _%>, import.meta.url))<%_ } _%>),
102105
tracing: Tracing.ACTIVE,
103106
timeout: Duration.seconds(30),
104107
...props,
@@ -138,7 +141,7 @@ export class <%- operation.operationIdPascalCase %>Function extends <% if (isJav
138141
<%_ } else if (isJava) { _%>
139142
handler: "<%- metadata['x-handlers-java-package'] %>.<%- operation.operationIdPascalCase %>Handler",
140143
<%_ } _%>
141-
code: Code.fromAsset(path.resolve(__dirname, "..",
144+
code: Code.fromAsset(<%_ if (metadata.esm) { _%>url.fileURLToPath(new URL(path.join("..",<%_ } else { _%>path.resolve(__dirname, "..",<%_ } %>
142145
<%_ if (isTypeScript) { _%>
143146
"<%- metadata['x-handlers-typescript-asset-path'] %>",
144147
"<%- operation.operationIdKebabCase %>",
@@ -147,7 +150,7 @@ export class <%- operation.operationIdPascalCase %>Function extends <% if (isJav
147150
<%_ } else if (isJava) { _%>
148151
"<%- metadata['x-handlers-java-asset-path'] %>",
149152
<%_ } _%>
150-
)),
153+
)<%_ if (metadata.esm) { _%>, import.meta.url))<%_ } _%>),
151154
tracing: Tracing.ACTIVE,
152155
timeout: Duration.seconds(30),
153156
...props,

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-cdk-infrastructure/templates/index.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"ext": ".ts",
77
"overwrite": true
88
}
9-
###/TSAPI_WRITE_FILE###export * from "./api";
10-
export * from "./functions";
11-
export * from "./mock-integrations";
9+
###/TSAPI_WRITE_FILE###export * from "./api<%_ if (metadata.esm) { _%>.js<%_ } _%>";
10+
export * from "./functions<%_ if (metadata.esm) { _%>.js<%_ } _%>";
11+
export * from "./mock-integrations<%_ if (metadata.esm) { _%>.js<%_ } _%>";

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-lambda-handlers/templates/tests.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
} from "<%= metadata.runtimePackageName %>";
1414
import {
1515
<%- operation.name %>
16-
} from "../src/<%- operation.operationIdKebabCase %>";
16+
} from "../src/<%- operation.operationIdKebabCase %><%_ if (metadata.esm) { _%>.js<%_ } _%>";
1717
1818
// Common request arguments
1919
const requestArguments = {
@@ -24,7 +24,7 @@ const requestArguments = {
2424
context: {} as any,
2525
interceptorContext: {
2626
logger: {
27-
info: jest.fn(),
27+
info: <% if (metadata.vitest) { %>vi<% } else { %>jest<% } %>.fn(),
2828
},
2929
},
3030
} satisfies Omit<<%- operation.operationIdPascalCase %>ChainedRequestInput, 'input'>;

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-runtime/templates/index.ejs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"ext": ".ts",
66
"overwrite": true
77
}
8-
###/TSAPI_WRITE_FILE###export * from './models';
9-
export * from './server/operation-config';
10-
export * from './server/server-sdk';
11-
export * from './interceptors'
8+
###/TSAPI_WRITE_FILE###export * from './models<%_ if (metadata.esm) { _%>/index.js<%_ } _%>';
9+
export * from './server/operation-config<%_ if (metadata.esm) { _%>.js<%_ } _%>';
10+
export * from './server/server-sdk<%_ if (metadata.esm) { _%>.js<%_ } _%>';
11+
export * from './interceptors<%_ if (metadata.esm) { _%>/index.js<%_ } _%>'

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-runtime/templates/server/interceptors.ejs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88
###/TSAPI_WRITE_FILE###import {
99
PayloadlessChainedRequestInput,
10-
} from '..';
10+
} from '..<%_ if (metadata.esm) { _%>/index.js<%_ } _%>';
1111

1212
/**
1313
* Create an interceptor which catches any unhandled exceptions
@@ -39,7 +39,7 @@ export const tryCatchInterceptor = buildTryCatchInterceptor();
3939
"overwrite": true
4040
}
4141
###/TSAPI_WRITE_FILE###import { Logger } from '@aws-lambda-powertools/logger';
42-
import { PayloadlessChainedRequestInput, ChainedRequestInput } from '../..';
42+
import { PayloadlessChainedRequestInput, ChainedRequestInput } from '../..<%_ if (metadata.esm) { _%>/index.js<%_ } _%>';
4343

4444
const logger = new Logger();
4545

@@ -78,7 +78,7 @@ export class LoggingInterceptor {
7878
"overwrite": true
7979
}
8080
###/TSAPI_WRITE_FILE###import { Tracer } from '@aws-lambda-powertools/tracer';
81-
import { PayloadlessChainedRequestInput, ChainedRequestInput } from '../..';
81+
import { PayloadlessChainedRequestInput, ChainedRequestInput } from '../..<%_ if (metadata.esm) { _%>/index.js<%_ } _%>';
8282

8383
const tracer = new Tracer();
8484

@@ -149,7 +149,7 @@ export class TracingInterceptor {
149149
"overwrite": true
150150
}
151151
###/TSAPI_WRITE_FILE###import { Metrics } from '@aws-lambda-powertools/metrics';
152-
import { PayloadlessChainedRequestInput, ChainedRequestInput } from '../..';
152+
import { PayloadlessChainedRequestInput, ChainedRequestInput } from '../..<%_ if (metadata.esm) { _%>/index.js<%_ } _%>';
153153

154154
const metrics = new Metrics();
155155

@@ -191,15 +191,15 @@ export class MetricsInterceptor {
191191
"ext": ".ts",
192192
"overwrite": true
193193
}
194-
###/TSAPI_WRITE_FILE###import { LoggingInterceptor } from './powertools/logger';
195-
import { MetricsInterceptor } from './powertools/metrics';
196-
import { TracingInterceptor } from './powertools/tracer';
197-
import { tryCatchInterceptor } from './try-catch';
198-
199-
export * from './try-catch';
200-
export * from './powertools/tracer';
201-
export * from './powertools/metrics';
202-
export * from './powertools/logger';
194+
###/TSAPI_WRITE_FILE###import { LoggingInterceptor } from './powertools/logger<%_ if (metadata.esm) { _%>.js<%_ } _%>';
195+
import { MetricsInterceptor } from './powertools/metrics<%_ if (metadata.esm) { _%>.js<%_ } _%>';
196+
import { TracingInterceptor } from './powertools/tracer<%_ if (metadata.esm) { _%>.js<%_ } _%>';
197+
import { tryCatchInterceptor } from './try-catch<%_ if (metadata.esm) { _%>.js<%_ } _%>';
198+
199+
export * from './try-catch<%_ if (metadata.esm) { _%>.js<%_ } _%>';
200+
export * from './powertools/tracer<%_ if (metadata.esm) { _%>.js<%_ } _%>';
201+
export * from './powertools/metrics<%_ if (metadata.esm) { _%>.js<%_ } _%>';
202+
export * from './powertools/logger<%_ if (metadata.esm) { _%>.js<%_ } _%>';
203203

204204
/**
205205
* All default interceptors, for logging, tracing, metrics, and error handling

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-runtime/templates/server/operationConfig.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import {
1313
<%- model.name %>FromJSON,
1414
<%- model.name %>ToJSON,
1515
<%_ }); _%>
16-
} from '../models';
16+
} from '../models<%_ if (metadata.esm) { _%>/index.js<%_ } _%>';
1717

1818
<%_ const serviceClassName = services[0] ? services[0].className : "DefaultApi"; _%>
1919
// API Gateway Types
2020
import { APIGatewayProxyWebsocketEventV2, APIGatewayProxyResultV2, Context } from "aws-lambda";
21-
import { <%- serviceClassName %>ServerSdk } from "./server-sdk";
21+
import { <%- serviceClassName %>ServerSdk } from "./server-sdk<%_ if (metadata.esm) { _%>.js<%_ } _%>";
2222

2323
<%_ const toServerOperations = allOperations.filter(op => op.vendorExtensions && op.vendorExtensions['x-async'] && ['client_to_server', 'bidirectional'].includes(op.vendorExtensions['x-async'].direction)); _%>
2424
// Generic type for object keyed by operation names

packages/type-safe-api/scripts/type-safe-api/generators/typescript-async-runtime/templates/server/serverSdk.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
<%- model.name %>FromJSON,
1414
<%- model.name %>ToJSON,
1515
<%_ }); _%>
16-
} from "../models";
16+
} from "../models<%_ if (metadata.esm) { _%>/index.js<%_ } _%>";
1717
import {
1818
ApiGatewayManagementApiClient,
1919
PostToConnectionCommand,

packages/type-safe-api/scripts/type-safe-api/generators/typescript-cdk-infrastructure/templates/api.ejs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
###/TSAPI_WRITE_FILE###import { TypeSafeRestApi, TypeSafeRestApiProps, TypeSafeApiIntegration } from "@aws/pdk/type-safe-api";
1010
import { Construct } from "constructs";
1111
import { OperationLookup, OperationConfig } from "<%- metadata.runtimePackageName %>";
12+
<%_ if (metadata.esm) { _%>
13+
import * as url from 'url';
14+
<%_ } else { _%>
1215
import * as path from "path";
16+
<%_ } _%>
1317

1418
export type ApiIntegrations = OperationConfig<TypeSafeApiIntegration>;
1519

@@ -26,7 +30,11 @@ export class Api extends TypeSafeRestApi {
2630
super(scope, id, {
2731
...props,
2832
integrations: props.integrations as any,
33+
<%_ if (metadata.esm) { _%>
34+
specPath: url.fileURLToPath(new URL("<%- metadata.relativeSpecPath %>", import.meta.url)),
35+
<%_ } else { _%>
2936
specPath: path.resolve(__dirname, "<%- metadata.relativeSpecPath %>"),
37+
<%_ } _%>
3038
operationLookup: OperationLookup as any,
3139
});
3240
}

packages/type-safe-api/scripts/type-safe-api/generators/typescript-cdk-infrastructure/templates/functions.ejs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { Duration } from "aws-cdk-lib";
1010
import { SnapStartFunction, SnapStartFunctionProps } from "@aws/pdk/type-safe-api";
1111
import { Code, Function, Runtime, Tracing, FunctionProps } from "aws-cdk-lib/aws-lambda";
1212
import * as path from "path";
13+
<%_ if (metadata.esm) { _%>
14+
import * as url from 'url';
15+
<%_ } _%>
1316

1417
<%_ allOperations.forEach((operation) => { _%>
1518
<%_ if (operation.vendorExtensions && operation.vendorExtensions['x-handler']) { _%>
@@ -42,7 +45,7 @@ export class <%- operation.operationIdPascalCase %>Function extends <% if (isJav
4245
<%_ } else if (isJava) { _%>
4346
handler: "<%- metadata['x-handlers-java-package'] %>.<%- operation.operationIdPascalCase %>Handler",
4447
<%_ } _%>
45-
code: Code.fromAsset(path.resolve(__dirname, "..",
48+
code: Code.fromAsset(<%_ if (metadata.esm) { _%>url.fileURLToPath(new URL(path.join("..",<%_ } else { _%>path.resolve(__dirname, "..",<%_ } %>
4649
<%_ if (isTypeScript) { _%>
4750
"<%- metadata['x-handlers-typescript-asset-path'] %>",
4851
"<%- operation.operationIdKebabCase %>",
@@ -51,7 +54,7 @@ export class <%- operation.operationIdPascalCase %>Function extends <% if (isJav
5154
<%_ } else if (isJava) { _%>
5255
"<%- metadata['x-handlers-java-asset-path'] %>",
5356
<%_ } _%>
54-
)),
57+
)<%_ if (metadata.esm) { _%>, import.meta.url))<%_ } _%>),
5558
tracing: Tracing.ACTIVE,
5659
timeout: Duration.seconds(30),
5760
...props,

0 commit comments

Comments
 (0)