Skip to content

Commit

Permalink
add initial
Browse files Browse the repository at this point in the history
  • Loading branch information
karimsa committed Sep 8, 2021
0 parents commit 1f1776a
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
*.log
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/ctxz.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require('path')
const { build } = require('esbuild')

const buildFile = (input, output) =>
build({
entryPoints: [input],
outfile: output,
bundle: true,
platform: 'node',
target: 'node14',
plugins: [
{
name: 'make-all-packages-external',
setup(build) {
let filter = /^[^./]|^\.[^./]|^\.\.[^/]/
build.onResolve({ filter }, (args) => ({
path: args.path,
external: true,
}))
},
},
],
watch: process.argv.includes('-w'),
})

buildFile(`./src/index.ts`, `./dist/index.js`).catch((error) => {
if (!error.errors) {
console.error(error)
}
process.exit(1)
})
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@karimsa/context",
"version": "0.0.0",
"main": "./dist/index.js",
"scripts": {
"start": "node ./dist/index.js",
"build": "tsc && node esbuild"
},
"devDependencies": {
"@types/node": "^16.7.13",
"@types/uuid": "^8.3.1",
"esbuild": "^0.12.25",
"typescript": "^4.4.2"
},
"dependencies": {
"uuid": "^8.3.2"
}
}
52 changes: 52 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as uuid from 'uuid';

class OperationContextEntry {
private readonly trace: Error;
private readonly contextValues: Record<string, any> = {};

constructor(private readonly context: OperationContext) {
this.trace = new Error('-----');
}

next() {
return this.context.next()
}

addContext(key: string, value: any): OperationContextEntry {
this.contextValues[key] = value
return this
}

addHttpRequest(request: { method: string; url: string; body: any }, response: { status: number; body: any }): OperationContextEntry {
this.addContext('request', { method: request.method, url: request.method, body: request.body });
this.addContext('response', { status: response.status, body: response.body })
return this
}

toJSON() {
const stacktrace = String(this.trace.stack || this.trace).split('\n');
return {
values: this.contextValues,
// Remove the first line, it has an empty error message
stacktrace: stacktrace.slice(1).map(line => line.trim()),
};
}
}

export class OperationContext {
private readonly id: string = uuid.v4();
private trace: OperationContextEntry[] = [];

next(): OperationContextEntry {
const entry = new OperationContextEntry(this);
this.trace.push(entry);
return entry;
}

toJSON() {
return {
id: this.id,
trace: this.trace.map(entry => entry.toJSON()),
}
}
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["esnext"],
"module": "commonjs",
"moduleResolution": "node",
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"declaration": true,
"declarationDir": "./dist"
}
}
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/node@^16.7.13":
version "16.7.13"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.13.tgz#86fae356b03b5a12f2506c6cf6cd9287b205973f"
integrity sha512-pLUPDn+YG3FYEt/pHI74HmnJOWzeR+tOIQzUx93pi9M7D8OE7PSLr97HboXwk5F+JS+TLtWuzCOW97AHjmOXXA==

"@types/uuid@^8.3.1":
version "8.3.1"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.1.tgz#1a32969cf8f0364b3d8c8af9cc3555b7805df14f"
integrity sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==

esbuild@^0.12.25:
version "0.12.25"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.12.25.tgz#c2131cef022cf9fe94aaa5e00110b27fc976221a"
integrity sha512-woie0PosbRSoN8gQytrdCzUbS2ByKgO8nD1xCZkEup3D9q92miCze4PqEI9TZDYAuwn6CruEnQpJxgTRWdooAg==

typescript@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86"
integrity sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

0 comments on commit 1f1776a

Please sign in to comment.