Skip to content

Commit 2e44dd5

Browse files
authored
Merge pull request #1 from speakeasy-api/kevincai/SPE-265-initial-setup
Creating simple typescript repository
2 parents a72a197 + 5caa7da commit 2e44dd5

File tree

16 files changed

+3460
-1
lines changed

16 files changed

+3460
-1
lines changed

.gitignore

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

.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+
{}

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
# speakeasy-typescript-sdk
1+
# speakeasy-typescript-sdk
2+
3+
## Setting up
4+
5+
Feel free to use any system to manage your Node installing, but a `.nvmrc` is included in the library for `nvm` support.
6+
7+
To setup:
8+
9+
1. Install `nvm` following instructions on (nvm repo)[https://github.com/nvm-sh/nvm]
10+
2. Install the correct node version with `nvm install`
11+
3. Make sure the symlinks are correct with `nvm use`
12+
13+
## Formating, Linting & Testing
14+
15+
`prettier` is used with `husky` and `lint-staged` to lint on commit, which should be installed automatically by `npm`.
16+
17+
Use `yarn test` to run the test suite.

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/express": "^4.17.13",
10+
"@types/har-format": "^1.2.8",
11+
"@types/jest": "^28.1.6",
12+
"@types/node": "^18.6.0",
13+
"@types/supertest": "^2.0.12",
14+
"husky": "^8.0.1",
15+
"jest": "^28.1.3",
16+
"lint-staged": "^13.0.3",
17+
"prettier": "^2.7.1",
18+
"supertest": "^6.2.4",
19+
"ts-jest": "^28.0.7",
20+
"ts-node": "^10.9.1",
21+
"typescript": "^4.7.4"
22+
},
23+
"peerDepedencies": {
24+
"express": "^4.18.1"
25+
},
26+
"scripts": {
27+
"build": "yarn tsc",
28+
"test": "yarn jest",
29+
"prepare": "yarn husky install"
30+
},
31+
"dependencies": {},
32+
"engines": {
33+
"node": ">=16.0.0"
34+
},
35+
"lint-staged": {
36+
"**/*": "prettier --write --ignore-unknown"
37+
}
38+
}

src/client.ts

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

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 { SpeakeasySDK } 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+
}

0 commit comments

Comments
 (0)