Skip to content

Commit 5f5903b

Browse files
Merge pull request #3 from traceo-dev/feat/browser-sdks
Feat/browser sdks
2 parents 6a1b4c9 + 1473c6a commit 5f5903b

38 files changed

Lines changed: 503 additions & 85 deletions

.prettierrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"printWidth": 98,
5+
"semi": true,
6+
"quoteProps": "as-needed",
7+
"bracketSpacing": true,
8+
"arrowParens": "always",
9+
"endOfLine": "lf",
10+
"singleQuote": false
11+
}

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
3+
"version": "0.31.8",
34
"npmClient": "yarn",
4-
"version": "0.31.5",
55
"useWorkspaces": true
66
}

package.json

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"name": "traceo-sdk",
44
"private": true,
55
"scripts": {
6+
"bootstrap": "lerna bootstrap",
67
"build": "lerna run build",
7-
"build:tarball": "lerna run build:tarball"
8+
"build:tarball": "lerna run build:tarball",
9+
"lint:prettier": "lerna run lint:prettier"
810
},
911
"workspaces": [
10-
"packages/node"
12+
"packages/node",
13+
"packages/browser",
14+
"packages/react"
1115
],
1216
"devDependencies": {
1317
"@types/node": "10.17.0",
@@ -20,14 +24,20 @@
2024
"@types/jest": "^29.2.0",
2125
"eslint": "^8.8.0",
2226
"jest": "^26.5.5",
23-
"npm-run-all": "^4.1.2",
2427
"os": "^0.1.2",
25-
"prettier": "^2.5.1",
2628
"prettier-check": "^2.0.0",
27-
"rimraf": "^3.0.2",
2829
"rollup": "^2.66.1",
2930
"ts-node": "^10.4.0",
30-
"tslint": "^5.11.0"
31+
"tslint": "^5.11.0",
32+
"@types/react": "^16.9.49",
33+
"@types/react-dom": "^16.9.8",
34+
"lint-staged": "^10.5.3",
35+
"npm-run-all": "^4.1.5",
36+
"prettier": "2.1.2",
37+
"react": "^18.1.0",
38+
"react-dom": "^18.1.0",
39+
"rimraf": "^3.0.2",
40+
"webpack": "^5.11.0"
3141
},
3242
"jest": {
3343
"verbose": false,

packages/browser/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Traceo SDK Browser

packages/browser/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@traceo-sdk/browser",
3+
"version": "0.31.8",
4+
"main": "dist/index.js",
5+
"types": "dist/index.d.ts",
6+
"license": "MIT",
7+
"files": [
8+
"dist"
9+
],
10+
"homepage": "https://github.com/traceo-dev/traceo-sdk",
11+
"repository": {
12+
"type": "git",
13+
"url": "git://github.com/traceo-dev/traceo-sdk.git"
14+
},
15+
"scripts": {
16+
"build": "tsc -p ./tsconfig.json --outDir dist",
17+
"build:tarball": "yarn build && npm pack",
18+
"prebuild": "rimraf ./dist",
19+
"lint": "run-s lint:prettier",
20+
"lint:prettier": "prettier ./src/**/*.{js,ts,tsx} --write",
21+
"prepack": "yarn lint",
22+
"test": "jest",
23+
"test:watch": "jest --watch --notify"
24+
},
25+
"dependencies": {
26+
"bowser": "2.11.0"
27+
},
28+
"publishConfig": {
29+
"access": "public"
30+
},
31+
"browser": {
32+
"http": false,
33+
"https": false
34+
}
35+
}

packages/browser/src/client.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { IBrowserClient, TraceoOptions } from "./types/client";
2+
import { Transport } from "./transport";
3+
import { utils } from "./utils";
4+
5+
export abstract class BrowserClient implements IBrowserClient {
6+
public headers!: { [key: string]: any };
7+
public options: TraceoOptions;
8+
public transport: Transport;
9+
10+
constructor(options: TraceoOptions) {
11+
this.options = options;
12+
this.transport = new Transport(this.options);
13+
14+
this.initSDK();
15+
}
16+
17+
public abstract postInitSDK(): void;
18+
19+
public sendError(error: Error): void {
20+
const browser = utils.browserDetails();
21+
this.transport.send({
22+
type: error.name,
23+
message: error.message,
24+
stack: error.stack as string,
25+
browser
26+
}, this.headers);
27+
}
28+
29+
private initSDK(): void {
30+
this.postInitSDK();
31+
}
32+
}

packages/browser/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { BrowserClient } from "./client";
2+
export type {
3+
Dictionary,
4+
IBrowserClient,
5+
TraceoOptions,
6+
TraceoBrowserError as TraceoError
7+
} from "./types/client";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ITransport, RequestOptions } from "../types/transport";
2+
3+
export class FetchTransport implements ITransport {
4+
private _options: RequestOptions;
5+
6+
constructor(options: RequestOptions) {
7+
this._options = options;
8+
}
9+
10+
public async request(): Promise<any> {
11+
const options: RequestInit = {
12+
method: this._options.method,
13+
headers: this._options.headers,
14+
body: JSON.stringify(this._options.body)
15+
};
16+
17+
await fetch(this._options.url, options);
18+
}
19+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Dictionary, TraceoOptions } from "../types/client";
2+
import { RequestOptions } from "../types/transport";
3+
import { FetchTransport } from "./fetch";
4+
import { XhrTransport } from "./xhr";
5+
6+
export class Transport {
7+
private _options: TraceoOptions;
8+
9+
constructor(options: TraceoOptions) {
10+
this._options = options;
11+
}
12+
13+
public send(body: object, headers: Dictionary<string>) {
14+
try {
15+
const options = this.requestOptions(body, headers);
16+
this.transport(options).request();
17+
} catch (error) {
18+
console.log("Error sending data to traceo: ", error);
19+
}
20+
}
21+
22+
private transport(options: RequestOptions) {
23+
if (window.XMLHttpRequest && !window.fetch) {
24+
return new XhrTransport(options);
25+
}
26+
27+
return new FetchTransport(options);
28+
}
29+
30+
private requestOptions(body: {}, headers: Dictionary<string>): RequestOptions {
31+
const reqUrl = this.clientURL;
32+
33+
// http://localhost:3000/api/worker/incident/app-id
34+
const url = `${reqUrl.origin}/api/worker/incident/${this._options.appId}`;
35+
36+
return {
37+
protocol: reqUrl.protocol,
38+
headers: {
39+
"Content-Type": "application/json",
40+
...headers
41+
},
42+
host: reqUrl.hostname,
43+
method: "POST",
44+
url,
45+
port: reqUrl.port,
46+
body
47+
};
48+
}
49+
50+
private get clientURL(): URL {
51+
return new URL(this._options.url);
52+
}
53+
}

0 commit comments

Comments
 (0)