Skip to content

Commit

Permalink
Use Babel to support CommonJS and ESM (#1)
Browse files Browse the repository at this point in the history
* Use Babel to support CommonJS and ESM

* Type fixes
  • Loading branch information
itssimon committed Jan 8, 2024
1 parent fdc2711 commit 3995566
Show file tree
Hide file tree
Showing 32 changed files with 7,271 additions and 3,882 deletions.
9 changes: 2 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# dependencies
/node_modules

# testing
/coverage

# production
/dist
/lib
/node_modules

# misc
.DS_Store
Expand Down
10,847 changes: 7,062 additions & 3,785 deletions package-lock.json

Large diffs are not rendered by default.

101 changes: 95 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,51 @@
"url": "https://github.com/apitally/nodejs-client/issues"
},
"homepage": "https://apitally.io",
"main": "dist/index.js",
"type": "module",
"scripts": {
"build": "tsc",
"build:tsc": "tsc",
"build:cjs": "BABEL_ENV=cjs babel lib/tsc --out-dir lib/cjs --extensions \".js\"",
"build:esm": "BABEL_ENV=esm babel lib/tsc --out-dir lib/esm --extensions \".js\"",
"build:types": "tsc --emitDeclarationOnly --declaration --declarationMap --outDir lib/types",
"build": "npm run build:tsc && npm run build:cjs && npm run build:esm && npm run build:types",
"format": "prettier --write .",
"check": "tsc --noEmit && eslint src tests && prettier --check .",
"test": "jest"
},
"files": [
"dist/**/*",
"!dist/tests/",
"lib/cjs/",
"lib/esm/",
"lib/types/",
"README.md",
"LICENSE"
],
"exports": {
"./common": {
"import": "./lib/esm/common/index.js",
"require": "./lib/cjs/common/index.js",
"types": "./lib/types/common/index.d.ts"
},
"./express": {
"import": "./lib/esm/express/index.js",
"require": "./lib/cjs/express/index.js",
"types": "./lib/types/express/index.d.ts"
},
"./fastify": {
"import": "./lib/esm/fastify/index.js",
"require": "./lib/cjs/fastify/index.js",
"types": "./lib/types/fastify/index.d.ts"
},
"./koa": {
"import": "./lib/esm/koa/index.js",
"require": "./lib/cjs/koa/index.js",
"types": "./lib/types/koa/index.d.ts"
},
"./nestjs": {
"import": "./lib/esm/nestjs/index.js",
"require": "./lib/cjs/nestjs/index.js",
"types": "./lib/types/nestjs/index.d.ts"
}
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
"extends": [
Expand All @@ -62,9 +94,60 @@
},
"root": true
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"esmodules": true
}
}
]
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"version": "2023-05"
}
],
"@babel/plugin-transform-runtime"
],
"env": {
"cjs": {
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs"
}
]
]
},
"esm": {
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
]
}
},
"sourceMaps": true
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"transform": {
"^.+\\.ts$": "ts-jest",
"^.+\\.js$": "babel-jest"
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"coverageReporters": [
"text",
"lcovonly"
Expand All @@ -75,7 +158,7 @@
},
"dependencies": {
"axios": "^1",
"axios-retry": "^3",
"axios-retry": "^4",
"winston": "^3"
},
"peerDependencies": {
Expand Down Expand Up @@ -111,20 +194,26 @@
}
},
"devDependencies": {
"@babel/cli": "^7.23.4",
"@babel/core": "^7.23.7",
"@babel/plugin-proposal-decorators": "^7.23.7",
"@babel/plugin-transform-runtime": "^7.23.7",
"@babel/preset-env": "^7.23.7",
"@jest/globals": "^29.7.0",
"@koa/router": "^12.0.1",
"@nestjs/common": "^10.2.9",
"@nestjs/core": "^10.2.9",
"@nestjs/platform-express": "^10.2.9",
"@nestjs/testing": "^10.2.9",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.8",
"@types/koa": "^2.13.11",
"@types/koa__router": "^12.0.4",
"@types/koa-route": "^3.2.8",
"@types/node": "^20.9.0",
"@types/supertest": "^2.0.16",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"babel-jest": "^29.7.0",
"celebrate": "^15.0.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
12 changes: 6 additions & 6 deletions src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import axios, { AxiosError, AxiosInstance } from "axios";
import axiosRetry from "axios-retry";
import { randomUUID } from "crypto";

import { KeyCacheBase, KeyRegistry } from "./keyRegistry";
import { Logger, getLogger } from "./logging";
import RequestLogger from "./requestLogger";
import { KeyCacheBase, KeyRegistry } from "./keyRegistry.js";
import { Logger, getLogger } from "./logging.js";
import RequestLogger from "./requestLogger.js";
import {
ApitallyConfig,
AppInfo,
AppInfoPayload,
RequestsDataPayload,
} from "./types";
import { isValidClientId, isValidEnv } from "./utils";
import ValidationErrorLogger from "./validationErrorLogger";
} from "./types.js";
import { isValidClientId, isValidEnv } from "./utils.js";
import ValidationErrorLogger from "./validationErrorLogger.js";

const SYNC_INTERVAL = 60000; // 60 seconds
const REQUEST_TIMEOUT = 10000; // 10 seconds
Expand Down
2 changes: 1 addition & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { KeyCacheBase } from "./keyRegistry";
export { KeyCacheBase } from "./keyRegistry.js";
2 changes: 1 addition & 1 deletion src/common/requestLogger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestInfo, RequestsItem } from "./types";
import { RequestInfo, RequestsItem } from "./types.js";

export default class RequestLogger {
private requestCounts: Map<string, number>;
Expand Down
4 changes: 2 additions & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyCacheBase } from "./keyRegistry";
import { Logger } from "./logging";
import { KeyCacheBase } from "./keyRegistry.js";
import { Logger } from "./logging.js";

export type ApitallyConfig = {
clientId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/common/validationErrorLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ConsumerMethodPath,
ValidationError,
ValidationErrorsItem,
} from "./types";
} from "./types.js";

export default class ValidationErrorLogger {
private errorCounts: Map<string, number>;
Expand Down
4 changes: 2 additions & 2 deletions src/express/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextFunction, Request, Response } from "express";
import type { NextFunction, Request, Response } from "express";

import { ApitallyClient } from "../common/client";
import { ApitallyClient } from "../common/client.js";

export const requireApiKey = ({
scopes,
Expand Down
6 changes: 3 additions & 3 deletions src/express/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { KeyInfo } from "../common/keyRegistry";
export { requireApiKey } from "./auth";
export { useApitally } from "./middleware";
export { KeyInfo } from "../common/keyRegistry.js";
export { requireApiKey } from "./auth.js";
export { useApitally } from "./middleware.js";
2 changes: 1 addition & 1 deletion src/express/listEndpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,4 @@ const getEndpoints = function (app) {
);
};

module.exports = getEndpoints;
export default getEndpoints;
12 changes: 6 additions & 6 deletions src/express/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Express, NextFunction, Request, Response } from "express";
import type { Express, NextFunction, Request, Response } from "express";
import { performance } from "perf_hooks";

import { ApitallyClient } from "../common/client";
import { KeyInfo } from "../common/keyRegistry";
import { ApitallyConfig, AppInfo, ValidationError } from "../common/types";
import { getPackageVersion } from "../common/utils";
import listEndpoints from "./listEndpoints";
import { ApitallyClient } from "../common/client.js";
import { KeyInfo } from "../common/keyRegistry.js";
import { ApitallyConfig, AppInfo, ValidationError } from "../common/types.js";
import { getPackageVersion } from "../common/utils.js";
import listEndpoints from "./listEndpoints.js";

declare module "express" {
interface Request {
Expand Down
9 changes: 7 additions & 2 deletions src/fastify/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { DoneFuncWithErrOrRes, FastifyReply, FastifyRequest } from "fastify";
import { ApitallyClient } from "../common/client";
import type {
DoneFuncWithErrOrRes,
FastifyReply,
FastifyRequest,
} from "fastify";

import { ApitallyClient } from "../common/client.js";

export const requireApiKey = ({
scopes,
Expand Down
6 changes: 3 additions & 3 deletions src/fastify/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { KeyInfo } from "../common/keyRegistry";
export { requireApiKey } from "./auth";
export { default as apitallyPlugin } from "./plugin";
export { KeyInfo } from "../common/keyRegistry.js";
export { requireApiKey } from "./auth.js";
export { default as apitallyPlugin } from "./plugin.js";
16 changes: 8 additions & 8 deletions src/fastify/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FastifyInstance, FastifyRequest } from "fastify";
import type { FastifyPluginAsync, FastifyRequest } from "fastify";
import fp from "fastify-plugin";

import { ApitallyClient } from "../common/client";
import { KeyInfo } from "../common/keyRegistry";
import { ApitallyConfig, PathInfo, ValidationError } from "../common/types";
import { getPackageVersion } from "../common/utils";
import { ApitallyClient } from "../common/client.js";
import { KeyInfo } from "../common/keyRegistry.js";
import { ApitallyConfig, PathInfo, ValidationError } from "../common/types.js";
import { getPackageVersion } from "../common/utils.js";

declare module "fastify" {
interface FastifyReply {
Expand All @@ -17,9 +17,9 @@ declare module "fastify" {
}
}

const apitallyPlugin = async (
fastify: FastifyInstance,
config: ApitallyConfig,
const apitallyPlugin: FastifyPluginAsync<ApitallyConfig> = async (
fastify,
config,
) => {
const client = new ApitallyClient(config);
const routes: PathInfo[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/koa/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Koa from "koa";

import { ApitallyClient } from "../common/client";
import { ApitallyClient } from "../common/client.js";

export const requireApiKey = ({
scopes,
Expand Down
6 changes: 3 additions & 3 deletions src/koa/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { KeyInfo } from "../common/keyRegistry";
export { requireApiKey } from "./auth";
export { useApitally } from "./middleware";
export { KeyInfo } from "../common/keyRegistry.js";
export { requireApiKey } from "./auth.js";
export { useApitally } from "./middleware.js";
8 changes: 4 additions & 4 deletions src/koa/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Koa from "koa";

import { ApitallyClient } from "../common/client";
import { KeyInfo } from "../common/keyRegistry";
import { ApitallyConfig, AppInfo, PathInfo } from "../common/types";
import { getPackageVersion } from "../common/utils";
import { ApitallyClient } from "../common/client.js";
import { KeyInfo } from "../common/keyRegistry.js";
import { ApitallyConfig, AppInfo, PathInfo } from "../common/types.js";
import { getPackageVersion } from "../common/utils.js";

export const useApitally = (app: Koa, config: ApitallyConfig) => {
const client = new ApitallyClient(config);
Expand Down
4 changes: 2 additions & 2 deletions src/nestjs/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import { Reflector } from "@nestjs/core";
import { Request, Response } from "express";

import { ApitallyClient } from "../common/client";
import { KeyInfo } from "../common/keyRegistry";
import { ApitallyClient } from "../common/client.js";
import { KeyInfo } from "../common/keyRegistry.js";

export const Scopes = (...scopes: string[]) => SetMetadata("scopes", scopes);

Expand Down
6 changes: 3 additions & 3 deletions src/nestjs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { KeyInfo } from "../common/keyRegistry";
export { useApitally } from "../express";
export { ApitallyApiKeyGuard, GetKeyInfo, Scopes } from "./auth";
export { KeyInfo } from "../common/keyRegistry.js";
export { useApitally } from "../express/index.js";
export { ApitallyApiKeyGuard, GetKeyInfo, Scopes } from "./auth.js";
12 changes: 10 additions & 2 deletions tests/common/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import {
afterEach,
beforeAll,
describe,
expect,
it,
jest,
} from "@jest/globals";
import nock from "nock";

import { ApitallyClient } from "../../src/common/client";
import { APITALLY_HUB_BASE_URL, CLIENT_ID, ENV } from "../utils";
import { ApitallyClient } from "../../src/common/client.js";
import { APITALLY_HUB_BASE_URL, CLIENT_ID, ENV } from "../utils.js";

describe("Client", () => {
beforeAll(() => {
Expand Down
7 changes: 4 additions & 3 deletions tests/express/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
import { Express } from "express";
import request from "supertest";

import { ApitallyClient } from "../../src/common/client";
import { API_KEY, mockApitallyHub } from "../utils";
import { getAppWithCelebrate, getAppWithValidator } from "./app";
import { ApitallyClient } from "../../src/common/client.js";
import { API_KEY, mockApitallyHub } from "../utils.js";
import { getAppWithCelebrate, getAppWithValidator } from "./app.js";

const testCases = [
{
Expand Down
6 changes: 3 additions & 3 deletions tests/express/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Joi, Segments, celebrate, errors } from "celebrate";
import express from "express";
import { query, validationResult } from "express-validator";

import { KeyCacheBase } from "../../src/common/keyRegistry";
import { requireApiKey, useApitally } from "../../src/express";
import { CLIENT_ID, ENV } from "../utils";
import { KeyCacheBase } from "../../src/common/keyRegistry.js";
import { requireApiKey, useApitally } from "../../src/express/index.js";
import { CLIENT_ID, ENV } from "../utils.js";

class TestKeyCache extends KeyCacheBase {
private data: string | null = JSON.stringify({ salt: "xxx", keys: {} });
Expand Down
Loading

0 comments on commit 3995566

Please sign in to comment.