Skip to content

Commit 372bbda

Browse files
committed
feat(core): addbasic fn
1 parent 250105e commit 372bbda

File tree

13 files changed

+2127
-63
lines changed

13 files changed

+2127
-63
lines changed

package-lock.json

Lines changed: 1391 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@
22
"name": "root",
33
"private": true,
44
"scripts": {
5-
"lerna": "lerna"
5+
"lerna": "lerna",
6+
"format:code": "prettier --write \"packages/**/*.{ts,js,?css}\"",
7+
"format": "npm-run-all -p format:code"
8+
},
9+
"husky": {
10+
"hooks": {
11+
"pre-commit": "lint-staged"
12+
}
13+
},
14+
"lint-staged": {
15+
"packages/**/*.{ts,js,?css,json}": [
16+
"prettier --write",
17+
"git add"
18+
],
19+
"packages/**/*.{ts,js}": [
20+
"tslint -p tsconfig.json -c tslint.json --fix",
21+
"git add"
22+
]
623
},
724
"devDependencies": {
8-
"lerna": "^3.21.0"
25+
"codelyzer": "^5.2.2",
26+
"husky": "^4.2.5",
27+
"lerna": "^3.21.0",
28+
"lint-staged": "^10.2.6",
29+
"npm-run-all": "^4.1.5",
30+
"prettier": "^2.0.5",
31+
"tslint": "^6.1.2"
932
},
1033
"dependencies": {
1134
"@types/node": "^14.0.5",
Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
1-
import fetch from 'cross-fetch';
2-
import { Transaction } from '../transaction/transaction';
1+
import fetch from "cross-fetch";
2+
import { Transaction } from "../transaction/transaction";
3+
import { Type } from "../enums/enums";
34

4-
class Application {
5-
constructor(private id: string, private parentId: string = "") {}
6-
7-
createTransaction(name: string): Transaction {
8-
return new Transaction(name, this.id)
9-
}
5+
export class Application {
6+
constructor(private id: string) {}
107

8+
createTransaction<T>(
9+
name: string,
10+
type: Type,
11+
fn: (trx: Transaction<T>) => T
12+
): Transaction<T> {
13+
return new Transaction(
14+
{
15+
name,
16+
applicationId: this.id,
17+
type,
18+
},
19+
fn
20+
);
21+
}
1122
}
1223

13-
export function createApplication(monitoringHost: string, name: string, host: string): Promise<Application> {
14-
return fetch(`${monitoringHost}/v1/applications`, {
15-
method: "POST",
16-
body: JSON.stringify({
17-
name,
18-
host,
19-
})
20-
}).then<{data: {
21-
application_id: string
22-
}}>((body) => body.json())
23-
.then((res) => new Application(res.data.application_id))
24-
}
24+
export function createApplication(
25+
monitoringHost: string,
26+
name: string,
27+
host: string
28+
): Promise<Application> {
29+
return fetch(`${monitoringHost}/v1/applications`, {
30+
method: "POST",
31+
body: JSON.stringify({
32+
name,
33+
host,
34+
}),
35+
})
36+
.then<{
37+
data: {
38+
application_id: string;
39+
};
40+
}>((body) => body.json())
41+
.then((res) => new Application(res.data.application_id));
42+
}

packages/core/lib/enums/enums.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export enum Status {
2+
TRANSACTION_SUCCESSFUL = 1,
3+
TRANSACTION_FAILED = 2,
4+
}
5+
6+
export enum Type {
7+
TRANSACTION_TYPE_UNSPECIFIED = 0,
8+
TRANSACTION_TYPE_XHR = 1,
9+
TRANSACTION_TYPE_FETCH = 2,
10+
TRANSACTION_TYPE_WEBSOCKET = 3,
11+
TRANSACTION_TYPE_HTTP = 4,
12+
TRANSACTION_TYPE_GRPC = 5,
13+
TRANSACTION_TYPE_DB = 6,
14+
TRANSACTION_TYPE_INTERNAL = 7,
15+
TRANSACTION_TYPE_ROUTER = 8,
16+
}

packages/core/lib/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./application/application";
2+
export * from "./enums/enums";
3+
export * from "./transaction/transaction";
Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
1-
import { nanoid } from 'nanoid'
1+
import { nanoid } from "nanoid";
2+
import { Type, Status } from "../enums/enums";
23

3-
export class Transaction {
4+
export interface TransactionOpts {
5+
name: string;
6+
applicationId: string;
7+
parent?: Transaction<any>;
8+
type: Type;
9+
}
410

5-
private id = nanoid()
11+
export interface CommitMsg {
12+
error?: Error;
13+
meta?: {
14+
host: string;
15+
path: string;
16+
};
17+
status: Status;
18+
}
619

7-
constructor(
8-
private name: string,
9-
private applicationId: string,
10-
private parentId: string = ""
11-
){
12-
}
20+
export class Transaction<T> {
21+
protected id = nanoid();
22+
23+
constructor(
24+
protected opts: TransactionOpts,
25+
protected fn: (trx: Transaction<T>) => T
26+
) {}
1327

14-
createTransaction(name: string): Transaction {
15-
return new Transaction(name, this.applicationId, this.id)
28+
commit(): T {
29+
const startTime = new Date();
30+
try {
31+
// if okey
32+
const result = this.fn(this);
33+
this._commit(startTime, new Date());
34+
return result;
35+
} catch (err) {
36+
// if throw error commit and send error
37+
this._commit(startTime, new Date(), {
38+
status: Status.TRANSACTION_FAILED,
39+
error: err,
40+
});
41+
throw err;
1642
}
17-
}
43+
}
44+
45+
getId() {
46+
return this.id;
47+
}
48+
49+
getApplicationId() {
50+
return this.opts.applicationId;
51+
}
52+
53+
protected _commit(from: Date, to: Date, msg?: CommitMsg) {}
54+
55+
createTransaction<R>(
56+
name: string,
57+
type: Type,
58+
fn: (trx: Transaction<R>) => R
59+
): Transaction<R> {
60+
return new Transaction(
61+
{
62+
name,
63+
parent: this,
64+
applicationId: this.opts.applicationId,
65+
type,
66+
},
67+
fn
68+
);
69+
}
70+
}

packages/express/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `express`
2+
3+
> TODO: description
4+
5+
## Usage
6+
7+
```
8+
const express = require('express');
9+
10+
// TODO: DEMONSTRATE API
11+
```

packages/express/lib/index.ts

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Request, Response, NextFunction, response } from "express";
2+
import { Application, Type, Transaction } from "@squzy/core";
3+
4+
class ExpressTransaction<T> extends Transaction<T> {
5+
commit() {
6+
const startTime = new Date();
7+
const res = this.fn(this);
8+
return res;
9+
}
10+
}
11+
12+
export function createMiddleware(app: Application) {
13+
return (req: Request, res: Response, next: NextFunction) => {
14+
const trx = app.createTransaction(
15+
req.baseUrl + req.path,
16+
Type.TRANSACTION_TYPE_ROUTER,
17+
() => next()
18+
);
19+
response.locals.__squzy_transaction = trx;
20+
trx.commit();
21+
};
22+
}

0 commit comments

Comments
 (0)