Skip to content

Commit 5c00210

Browse files
committed
Creating simple typescript repository
1 parent a72a197 commit 5c00210

File tree

15 files changed

+3441
-0
lines changed

15 files changed

+3441
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.DS_Store
3+
.husky

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v16.16.0

.prettierrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

jest.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
};

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "speakeasy-typescript-sdk",
3+
"version": "0.0.1",
4+
"main": "index.js",
5+
"repository": "git@github.com:speakeasy-api/speakeasy-typescript-sdk.git",
6+
"author": "",
7+
"license": "MIT",
8+
"devDependencies": {
9+
"@types/jest": "^28.1.6",
10+
"@types/supertest": "^2.0.12",
11+
"express": "^4.18.1",
12+
"husky": "^8.0.1",
13+
"jest": "^28.1.3",
14+
"lint-staged": "^13.0.3",
15+
"prettier": "^2.7.1",
16+
"supertest": "^6.2.4",
17+
"ts-jest": "^28.0.7",
18+
"ts-node": "^10.9.1",
19+
"typescript": "^4.7.4"
20+
},
21+
"scripts": {
22+
"build": "yarn tsc",
23+
"test": "yarn jest",
24+
"prepare": "yarn husky install"
25+
},
26+
"dependencies": {
27+
"@types/express": "^4.17.13",
28+
"@types/har-format": "^1.2.8",
29+
"@types/node": "^18.6.0",
30+
"har": "^1.0.0"
31+
},
32+
"engines": {
33+
"node": ">=16.0.0"
34+
},
35+
"lint-staged": {
36+
"**/*": "prettier --write --ignore-unknown"
37+
}
38+
}

src/client.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export type Config = {
2+
apiKey: string;
3+
};
4+
5+
export class SpeakeasyClient {
6+
apiKey: string;
7+
serverUrl: string;
8+
9+
public constructor(config: Config) {
10+
const { apiKey } = config;
11+
if (apiKey == null) {
12+
throw new Error("Speakeasy API key is required");
13+
}
14+
15+
this.apiKey = config.apiKey;
16+
17+
const serverUrl = process.env.SPEAKEASY_SERVER_URL;
18+
if (serverUrl == null) {
19+
throw new Error("Speakeasy Server URL is required");
20+
}
21+
22+
this.serverUrl = serverUrl;
23+
}
24+
}

src/error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function outputError(errMsg: string): void {
2+
console.error(errMsg);
3+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { SpeakeasyClient } from "./client";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Request, Response } from "express";
2+
import { getInstance } from "../../speakeasy";
3+
import { sendApiCall } from "../../transport";
4+
import { outputError } from "../../error";
5+
6+
export function expressMiddleware() {
7+
return function (req: Request, res: Response, next: () => void) {
8+
const speakeasy = getInstance();
9+
if (speakeasy == null) {
10+
outputError("Speakeasy has not been initialized");
11+
return next();
12+
}
13+
14+
res.on("finish", () => {
15+
sendApiCall();
16+
});
17+
next();
18+
};
19+
}

src/speakeasy.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Config, SpeakeasyClient } from "./client";
2+
import { outputError } from "./error";
3+
4+
let speakeasyInstance: SpeakeasyClient | null = null;
5+
6+
export function init(config: Config): SpeakeasyClient {
7+
if (speakeasyInstance != null) {
8+
outputError(
9+
"Speakeasy has already been initialized, skipping initialization"
10+
);
11+
return speakeasyInstance;
12+
}
13+
14+
speakeasyInstance = new SpeakeasyClient(config);
15+
return speakeasyInstance;
16+
}
17+
18+
export function getInstance(): SpeakeasyClient | null {
19+
return speakeasyInstance;
20+
}

0 commit comments

Comments
 (0)