Skip to content
Open
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
16 changes: 10 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
"extends": ["eslint:recommended", "prettier"],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
"sourceType": "script"
},
"rules": {},
"globals": {
"expect": true,
"it": true
}
"overrides": [
{
"files": ["src/**/*.js", "__tests__/**/*.mjs"],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
}
}
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ dump.rdb
# Coverage reports
.nyc_output
coverage

# Build output
dist
16 changes: 16 additions & 0 deletions .swcrc.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://swc.rs/schema.json",
"jsc": {
"parser": {
"syntax": "ecmascript",
"dynamicImport": true
},
"target": "es2018",
"keepClassNames": true
},
"module": {
"type": "commonjs",
"noInterop": false
},
"sourceMaps": false
}
17 changes: 17 additions & 0 deletions .swcrc.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://swc.rs/schema.json",
"jsc": {
"parser": {
"syntax": "ecmascript",
"dynamicImport": true
},
"target": "es2018",
"keepClassNames": true
},
"module": {
"type": "es6",
"strict": false,
"noInterop": false
},
"sourceMaps": false
}
51 changes: 51 additions & 0 deletions __tests__/esm-compat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

import createAPI from '../dist/esm/index.js';
import * as utils from '../dist/esm/lib/utils.js';
import prettyPrint from '../dist/esm/lib/prettyPrint.js';
import { ApiError } from '../dist/esm/lib/errors.js';

const event = {
httpMethod: 'GET',
path: '/compat',
headers: {},
multiValueHeaders: {},
};

async function main() {
const createApi = createAPI({ version: 'v1.0' });
createApi.get('/compat', (req, res) => {
res.json({ ok: true, method: req.method });
});

const result = await createApi.run(event, {});

if (typeof createAPI !== 'function') {
throw new Error('Expected default export to be a function');
}

if (typeof utils.escapeHtml !== 'function') {
throw new Error('Expected utils.escapeHtml to be a function');
}

if (typeof prettyPrint !== 'function') {
throw new Error('Expected prettyPrint default export to be a function');
}

if (typeof ApiError !== 'function') {
throw new Error('Expected ApiError export to be a function');
}

if (result.statusCode !== 200) {
throw new Error(`Expected statusCode 200, received ${result.statusCode}`);
}

if (JSON.parse(result.body).ok !== true) {
throw new Error('Expected successful ESM route response');
}
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
79 changes: 79 additions & 0 deletions __tests__/module-compat.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const { execFileSync } = require('child_process');
const path = require('path');

const event = {
httpMethod: 'GET',
path: '/compat',
headers: {},
multiValueHeaders: {},
};

const runRoute = async (api) => {
api.get('/compat', (req, res) => {
res.json({ ok: true, method: req.method });
});

return api.run(event, {});
};

describe('Module Compatibility Tests:', function () {
describe('CommonJS build output', function () {
it('loads the package factory from dist/cjs', function () {
const createAPI = require('../dist/cjs/index.js');
expect(typeof createAPI).toBe('function');
});

it('runs a route from dist/cjs', async function () {
const createAPI = require('../dist/cjs/index.js');
const api = createAPI({ version: 'v1.0' });
const result = await runRoute(api);

expect(result.statusCode).toBe(200);
expect(JSON.parse(result.body)).toEqual({ ok: true, method: 'GET' });
});

it('loads deep lib subpaths from dist/cjs', function () {
const utils = require('../dist/cjs/lib/utils.js');
const errors = require('../dist/cjs/lib/errors.js');
const prettyPrint = require('../dist/cjs/lib/prettyPrint.js');

expect(typeof utils.escapeHtml).toBe('function');
expect(typeof errors.ApiError).toBe('function');
expect(typeof prettyPrint).toBe('function');
});
});

describe('ESM build output', function () {
it('passes Node ESM compatibility checks', function () {
execFileSync(process.execPath, ['__tests__/esm-compat.mjs'], {
cwd: path.join(__dirname, '..'),
stdio: 'pipe',
});
});
});

describe('Package exports resolution', function () {
it('resolves the package root and lib subpaths', function () {
execFileSync(
process.execPath,
[
'-e',
"const { createRequire } = require('module');" +
"const packageRequire = createRequire(require('path').resolve('package.json'));" +
"const createAPI = packageRequire('.');" +
"const utils = packageRequire('lambda-api/lib/utils');" +
"const utilsWithExtension = packageRequire('lambda-api/lib/utils.js');" +
"if (typeof createAPI !== 'function') throw new Error('Expected package root export to be a function');" +
"if (typeof utils.escapeHtml !== 'function') throw new Error('Expected lib/utils export to expose escapeHtml');" +
"if (typeof utilsWithExtension.escapeHtml !== 'function') throw new Error('Expected lib/utils.js export to expose escapeHtml');",
],
{
cwd: path.join(__dirname, '..'),
stdio: 'pipe',
}
);
});
});
});
12 changes: 6 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectType, expectError } from 'tsd';
import { expectError, expectType } from 'tsd';
import {
API,
Request,
Expand All @@ -19,6 +19,7 @@ import {
FileError,
ApiError,
} from './index';
import createAPI from './index';
import {
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
Expand Down Expand Up @@ -55,6 +56,9 @@ const options: Options = {
};
expectType<Options>(options);

const apiFromFactory = createAPI(options);
expectType<API>(apiFromFactory);

const req = {} as Request;
expectType<string>(req.method);
expectType<string>(req.path);
Expand Down Expand Up @@ -230,11 +234,7 @@ const invalidTypedHandler: HandlerFunction<
TypedRequest,
Response<TypedResponseBody>
> = (req, res) => {
expectError(
res.json({
hello: req.params.thingId,
})
);
expectError(res.json({ hello: req.params.thingId }));
};
api.get<TypedRequest, Response<TypedResponseBody>>(
'/typed-invalid',
Expand Down
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
moduleNameMapper: {
'^\\.\\./index$': '<rootDir>/dist/cjs/index.js',
'^\\.\\./lib/(.*)$': '<rootDir>/dist/cjs/lib/$1.js',
},
testMatch: ['**/__tests__/**/*.unit.js'],
};
53 changes: 0 additions & 53 deletions lib/s3-service.js

This file was deleted.

Loading
Loading