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

feat(sdk-snippets): library modernization with ESM+CJS builds #1017

Closed
wants to merge 11 commits into from
5,418 changes: 3,630 additions & 1,788 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion packages/sdk-snippets/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"extends": [
"@readme/eslint-config",
"@readme/eslint-config/esm",
"@readme/eslint-config/typescript",
"@readme/eslint-config/testing/vitest"
],
"root": true,
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
},
"overrides": [
{
"files": ["src/fixtures/**/*.cjs"],
"rules": {
"import/no-commonjs": "off"
}
}
]
}
2 changes: 1 addition & 1 deletion packages/sdk-snippets/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ OVERWRITE_EVERYTHING=true npx jest src/targets/targets.test.ts

```ts
import type { Client } from '../../../targets';
import { CodeBuilder } from '@readme/httpsnippet/dist/helpers/code-builder';
import { CodeBuilder } from '@readme/httpsnippet/helpers/code-builder';

export const express: Client = {
info: {
Expand Down
55 changes: 34 additions & 21 deletions packages/sdk-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,58 @@
"name": "@readme/metrics-sdk-snippets",
"version": "2.4.1",
"description": "Generate code snippets for ReadMe Metrics SDKs in multiple languages",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "ISC",
"homepage": "https://readme.com/metrics",
"bugs": {
"email": "[email protected]",
"url": "https://github.com/readmeio/metrics-sdks/issues"
"sideEffects": false,
"type": "module",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"./package.json": "./package.json"
},
"repository": {
"type": "git",
"url": "[email protected]:readmeio/metrics-sdks.git"
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.cts",
"engines": {
"node": ">=18"
},
"keywords": [
"api-metrics",
"readme"
],
"engines": {
"node": ">=18"
"repository": {
"type": "git",
"url": "[email protected]:readmeio/metrics-sdks.git"
},
"homepage": "https://readme.com/metrics",
"bugs": {
"email": "[email protected]",
"url": "https://github.com/readmeio/metrics-sdks/issues"
},
"scripts": {
"build": "tsc --build tsconfig.build.json",
"clean": "rm -rf dist/",
"lint": "eslint . --ext js,ts",
"prebuild": "npm run clean",
"attw": "attw --pack --format table-flipped",
"build": "tsup",
"lint": "npm run lint:types && npm run lint:js",
"lint:js": "eslint . --ext js,ts",
"lint:types": "tsc --noEmit",
"prebuild": "rm -rf dist/",
"prepack": "npm run build",
"prettier": "prettier --list-different --write \"./**/**.{js,ts}\"",
"test": "vitest run --coverage"
},
"dependencies": {
"@readme/httpsnippet": "^6.2.1"
"@readme/httpsnippet": "^10.0.5"
},
"devDependencies": {
"@readme/eslint-config": "^14.0.0",
"@vitest/coverage-v8": "^0.34.2",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "^8.47.0",
"prettier": "^3.0.2",
"type-fest": "^4.2.0",
"typescript": "^5.1.6",
"vitest": "^0.34.2"
"prettier": "^3.3.2",
"tsup": "^8.1.0",
"type-fest": "^4.20.1",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"prettier": "@readme/eslint-config/prettier"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ exports[`code-converter > should convert code to a CodeBuilder instance 1`] = `
"
blank()

push(\\"import express from 'express';\\");
push("import express from 'express';");

blank()

push('const app = express();');

blank()

push(\\"app.get('/', (req, res) => {\\");
push("app.get('/', (req, res) => {");
push('res.sendStatus(200);', 1);
push('});');

Expand Down
10 changes: 3 additions & 7 deletions packages/sdk-snippets/src/helpers/code-builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type { CodeBuilderOptions } from '@readme/httpsnippet/dist/helpers/code-builder';

import { CodeBuilder as HTTPSnippetCodeBuilder } from '@readme/httpsnippet/dist/helpers/code-builder';

export type { CodeBuilderOptions };
import { CodeBuilder as HTTPSnippetCodeBuilder } from '@readme/httpsnippet/helpers/code-builder';

export class CodeBuilder extends HTTPSnippetCodeBuilder {
sections: {
Expand All @@ -29,7 +25,7 @@ export class CodeBuilder extends HTTPSnippetCodeBuilder {
};

endSection = (section: 'payload' | 'verification') => {
this.sections[section].end = this.code.length;
this.sections[section]!.end = this.code.length;
};

ranges = () => {
Expand Down Expand Up @@ -58,7 +54,7 @@ export class CodeBuilder extends HTTPSnippetCodeBuilder {
this.variables[opts.type] = {};
}

this.variables[opts.type][opts.name] = {
this.variables[opts.type]![opts.name] = {
line: this.code.length,
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-snippets/src/helpers/code-converter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';

import codeConverter from './code-converter';
import codeConverter from './code-converter.js';

const codeSample = `
import express from 'express';
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-snippets/src/helpers/code-converter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { basename } from 'node:path';
import process from 'process';
import process from 'node:process';

async function getStdin() {
if (process.stdin.isTTY) return null;
Expand All @@ -13,14 +13,14 @@ async function getStdin() {
return result.join('');
}

function processLine(line) {
function processLine(line: string) {
if (line === '') return '\nblank()\n';
const indentation = line.search(/\S|$/) / 2;
const quote = line.match(/'/) ? '"' : "'";
return `push(${quote}${line.trimLeft()}${quote}${indentation !== 0 ? `, ${indentation}` : ''});`;
}

export default function main(input) {
export default function main(input: string) {
return input
.toString()
.split('\n')
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-snippets/src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ClientInfo, TargetId, TargetInfo } from '../targets/targets';
import type { ClientInfo, TargetId, TargetInfo } from '../targets/targets.js';

import { targets } from '../targets/targets';
import { targets } from '../targets/targets.js';

export interface AvailableTarget extends TargetInfo {
clients: ClientInfo[];
Expand Down
13 changes: 7 additions & 6 deletions packages/sdk-snippets/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Variables } from '.';
import type { Variables } from './index.js';

import { describe, it, expect } from 'vitest';

import serverVariables from './fixtures/webhooks/server-variables';
import * as serverVariables from './fixtures/webhooks/server-variables.cjs';

import { MetricsSDKSnippet } from '.';
import { MetricsSDKSnippet } from './index.js';

describe('MetricsSDKSnippet', function () {
it('should return false if no matching target', function () {
const snippet = new MetricsSDKSnippet(serverVariables as Variables);
describe('MetricsSDKSnippet', () => {
it('should return false if no matching target', () => {
const snippet = new MetricsSDKSnippet(serverVariables as unknown as Variables);
// @ts-expect-error Testing a mistyped target
const result = snippet.convert('webhooks', null);

expect(result).toBe(false);
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk-snippets/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ClientId, SnippetType, TargetId } from './targets/targets';
import type { ClientId, SnippetType, TargetId } from './targets/targets.js';

import { targets } from './targets/targets';
import { targets } from './targets/targets.js';

export { availableServerTargets, availableWebhookTargets, extname } from './helpers/utils';
export { availableServerTargets, availableWebhookTargets, extname } from './helpers/utils.js';

export interface ServerVariable {
default?: string;
Expand Down Expand Up @@ -49,7 +49,7 @@ export class MetricsSDKSnippet {
const { convert } = target.services[snippetType][clientId || target.info.default];

// Reduce our variables into a set keyed by their type.
const { server, security } = this.variables.reduce(
const { server, security } = this.variables.reduce<any>(
(prev, next) => {
kanadgupta marked this conversation as resolved.
Show resolved Hide resolved
prev[next.source].push(next);
return prev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports[`webhooks > C# snippet generation > aws > AWS snippet generation with au
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 109,
},
"basic-auth": {
Expand Down Expand Up @@ -273,7 +273,7 @@ exports[`webhooks > C# snippet generation > aws > request should match fixture f
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 88,
},
"basic-auth": {
Expand Down Expand Up @@ -477,7 +477,7 @@ exports[`webhooks > C# snippet generation > dotnet6 > request should match fixtu
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 39,
},
"basic-auth": {
Expand Down Expand Up @@ -682,7 +682,7 @@ exports[`webhooks > Node.js snippet generation > aws > AWS snippet generation wi
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 72,
},
"basic-auth": {
Expand Down Expand Up @@ -892,7 +892,7 @@ exports[`webhooks > Node.js snippet generation > aws > request should match fixt
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 45,
},
"basic-auth": {
Expand Down Expand Up @@ -1096,7 +1096,7 @@ exports[`webhooks > Node.js snippet generation > express > request should match
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 30,
},
"basic-auth": {
Expand Down Expand Up @@ -1295,7 +1295,7 @@ exports[`webhooks > PHP snippet generation > laravel > request should match fixt
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 30,
},
"basic-auth": {
Expand Down Expand Up @@ -1500,7 +1500,7 @@ exports[`webhooks > Python snippet generation > aws > AWS snippet generation wit
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 64,
},
"basic-auth": {
Expand Down Expand Up @@ -1710,7 +1710,7 @@ exports[`webhooks > Python snippet generation > aws > request should match fixtu
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 49,
},
"basic-auth": {
Expand Down Expand Up @@ -1914,7 +1914,7 @@ exports[`webhooks > Python snippet generation > flask > request should match fix
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 42,
},
"basic-auth": {
Expand Down Expand Up @@ -2119,7 +2119,7 @@ exports[`webhooks > Ruby snippet generation > aws > AWS snippet generation with
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 68,
},
"basic-auth": {
Expand Down Expand Up @@ -2329,7 +2329,7 @@ exports[`webhooks > Ruby snippet generation > aws > request should match fixture
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 51,
},
"basic-auth": {
Expand Down Expand Up @@ -2533,7 +2533,7 @@ exports[`webhooks > Ruby snippet generation > rails > request should match fixtu
},
"variables": {
"security": {
"\\"petstore\\" auth": {
""petstore" auth": {
"line": 36,
},
"basic-auth": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// For use with AWS Lambda Runtime: dotnet6

import type { Client } from '../../../targets';
import type { Client } from '../../../targets.js';

import { CodeBuilder } from '../../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../../helpers/escape';
import { CodeBuilder } from '../../../../helpers/code-builder.js';
import { escapeForDoubleQuotes } from '../../../../helpers/escape.js';

export const aws: Client = {
info: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Client } from '../../../targets';
import type { Client } from '../../../targets.js';

import { CodeBuilder } from '../../../../helpers/code-builder';
import { escapeForObjectKey, escapeForDoubleQuotes } from '../../../../helpers/escape';
import { CodeBuilder } from '../../../../helpers/code-builder.js';
import { escapeForObjectKey, escapeForDoubleQuotes } from '../../../../helpers/escape.js';

export const dotnet6: Client = {
info: {
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-snippets/src/targets/csharp/target.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Target } from '../targets';
import type { Target } from '../targets.js';

import { aws } from './aws/webhooks/client';
import { dotnet6 } from './dotnet6/webhooks/client';
import { aws } from './aws/webhooks/client.js';
import { dotnet6 } from './dotnet6/webhooks/client.js';

export const csharp: Target = {
info: {
Expand Down
Loading
Loading