Implement as ts module and compile to js + d.ts file

To avoid the complications of separately implementing a JS module
and writing types for it and having consumers of the module do the
type definition assignement the module is now a typescript module which
compiles to javascript + a type definition file.
Signed-off-by: default avatarSnorre Magnus Davøen <snorre.davoen@nsd.uib.no>
parent 72e43ee6
Pipeline #5098 passed with stage
in 19 seconds
node_modules
\ No newline at end of file
node_modules
lib
\ No newline at end of file
......@@ -10,4 +10,4 @@ test:
- echo 'always-auth=true' >> ~/.npmrc
- npm set registry https://nexus.nsd.no/repository/npm-group/
- yarn install
- yarn run dtslint
\ No newline at end of file
- yarn compile
\ No newline at end of file
const schema = require('./log-schema/log-schema.json');
export default schema;
\ No newline at end of file
import schema from "./log-schema/log-schema.json";
export interface LogSchema {
/**
* The version of the log-schema. Used to separate versions into different Elasticsearch indices.
*/
schemaVersion: "v3";
/**
* The time of the event. Format: RFC 3339. (E.g. 1985-04-12T23:20:50.52Z)
*/
"@timestamp": string;
/**
* Log-level (DEBUG|INFO|WARN|ERROR).
*/
levelName: ("DEBUG" | "INFO" | "WARN" | "ERROR");
/**
* Same as the message parameter in log libraries. Should contain a message describing log event.
*/
message: string;
/**
* The process to which the log event pertains.
*/
process?: string;
/**
* The thread to which the log event pertains.
*/
thread?: string;
/**
* The application file to which the log event pertains.
*/
file?: string;
/**
* The line number to which the log event pertains.
*/
line?: number;
/**
* Identification of user to which log event pertains. Can be used to view log events for specific users.
*/
userId?: string;
/**
* Name of logger generating the log event.
*/
loggerName?: string;
/**
* Name of the service. E.g. user-service.
*/
serviceName: string;
/**
* Version of the service. E.g. v2.0.2.
*/
serviceVersion: string;
serviceStack?: string;
/**
* The id of a client/user request. Used to track a single request across services.
*/
xRequestId?: string;
/**
* String tags to help categorize and/or filter log events
*/
tags?: string[];
/**
* The current hostname for the service instance.
*/
host?: string;
/**
* The current port number for the service instance.
*/
port?: number;
/**
* A regular string or stringified JSON object containing data about log event.
*/
data?: string;
/**
* The URL from which the log event was initiated.
*/
url?: string;
/**
* The number of milliseconds spent on request/process.
*/
responseTime?: number;
/**
* HTTP status code if applicable.
*/
statusCode?: number;
/**
* HTTP method if applicable.
*/
method?: string;
error?: {
/**
* A string representation of error type, e.g. NO_SUCH_VARIABLE_ERROR.
*/
type?: string;
/**
* Error code uniquely identifiying error. E.g. 4324.
*/
code: string;
/**
* Name of service where error originated if the error originated outside this service.
*/
serviceName?: string;
/**
* Stack trace for the error. Should be santizied of sensitive information if possible.
*/
stack?: string;
/**
* Any data that may be related to error in string format.
*/
data?: string;
/**
* Regular string or stringified json object of original error object
*/
originalErrorObject?: string;
};
request?: {
/**
* Request headers like Accept, If-Modified-Since, etc. Avoid logging sensitive headers!
*/
headers?: {
[k: string]: string;
};
/**
* Query parameters contained within URL. E.g. (?myvalue=foo)
*/
query?: {
[k: string]: string | string[];
};
/**
* Path parameters contained whithin URL. E.g. /resource/<pathparam>
*/
path?: {
[k: string]: string;
};
/**
* The request payload/body if applicable (POST form, json payload, etc).
*/
payload?: string;
};
response?: {
/**
* Response headers like eTag, Content-Length, etc. Avoid logging sensitive headers!
*/
headers?: {
[k: string]: string;
};
/**
* A string representation of the response body. Do not log large bodies!
*/
body?: string;
};
}
const jsonSchema: LogSchema = schema;
export default schema;
\ No newline at end of file
{
"name": "log-schema-node",
"version": "1.0.0",
"main": "./log-schema/log-schema.json",
"types": "types",
"main": "lib/index.js",
"types": "lib/index",
"scripts": {
"dtslint": "dtslint types"
"compile": "tsc"
},
"repository": "git@gitlab.nsd.uib.no:nsd-commons/log-schema-node.git",
"author": "Snorre Magnus Davøen <snorre.davoen@nsd.uib.no>",
"license": "See LICENSE",
"files": [
"lib/index.d.ts",
"lib/index.js",
"log-schema/log-schema.json"
],
"devDependencies": {
"dtslint": "Microsoft/dtslint#production",
"jayschema": "^0.3.1",
"json-schema-to-typescript": "^4.2.0",
"dtslint": "Microsoft/dtslint#production"
"tslint": "^5.2.0",
"typescript": "^2.3.2"
}
}
import { LogSchema } from "../index";
const minimalEvent: LogSchema = {
schemaVersion: "v3",
"@timestamp": "1985-04-12T23:20:50.52Z",
levelName: "INFO",
message: "Ørliten melding",
serviceName: "some-service",
serviceVersion: "v1.0.0"
};
const fullEvent: LogSchema = {
schemaVersion: "v3",
"@timestamp": "1985-04-12T23:20:50.52Z",
levelName: "INFO",
message: "Ørliten melding",
userId: "some-user-id",
process: "main process id",
thread: "main thread id",
file: "main.java",
line: 42,
loggerName: "some-logger",
serviceName: "some-service",
serviceVersion: "v1.0.0",
serviceStack: "raird-stage-1",
host: "somehost",
port: 2100,
tags: [
"performance",
],
url: "/v0/hello",
responseTime: 120,
statusCode: 200,
method: "GET",
xRequestId: "cd88dba8-c8e1-4b2e-8a19-bc6220e0be65",
request: {
headers: {
"User-Agent": "curl/7.47.0",
Accept: "*/*"
},
path: {
param1: "param1-value",
param2: "param2-value"
},
query: {
query1: "query1-value",
query2: ["query2-value1", "query2-value2"]
},
payload: "{\"command\": \"tabulate\"}"
},
response: {
headers: {
"Content-Type": "application/octet-stream",
"X-Request-ID": "cd88dba8-c8e1-4b2e-8a19-bc6220e0be65",
"Content-Disposition": "attachment; filename=\"hello.bin\""
},
body: "{\"message\": \"Hello world!\"}"
},
error: {
type: "some-error",
code: "some-error-123",
serviceName: "some-external-service",
stack: "stacktrace1\nstacktrace2\nstacktrace3",
data: "more details...",
originalErrorObject: "stringobject"
},
data: "some-extra-string-data"
};
{
"compilerOptions": {
"declaration": true,
"outDir": "lib",
"target": "es5"
},
"exclude": [
"lib",
"node_modules"
]
}
\ No newline at end of file
{
"rules": {
"no-unused-expression": true,
"no-duplicate-variable": true,
"curly": true,
"class-name": true,
"semicolon": ["always"],
"triple-equals": true
}
}
\ No newline at end of file
export interface LogSchema {
/**
* The version of the log-schema. Used to separate versions into different Elasticsearch indices.
*/
schemaVersion: "v3";
/**
* The time of the event. Format: RFC 3339. (E.g. 1985-04-12T23:20:50.52Z)
*/
"@timestamp": string;
/**
* Log-level (DEBUG|INFO|WARN|ERROR).
*/
levelName: ("DEBUG" | "INFO" | "WARN" | "ERROR");
/**
* Same as the message parameter in log libraries. Should contain a message describing log event.
*/
message: string;
/**
* The process to which the log event pertains.
*/
process?: string;
/**
* The thread to which the log event pertains.
*/
thread?: string;
/**
* The application file to which the log event pertains.
*/
file?: string;
/**
* The line number to which the log event pertains.
*/
line?: number;
/**
* Identification of user to which log event pertains. Can be used to view log events for specific users.
*/
userId?: string;
/**
* Name of logger generating the log event.
*/
loggerName?: string;
/**
* Name of the service. E.g. user-service.
*/
serviceName: string;
/**
* Version of the service. E.g. v2.0.2.
*/
serviceVersion: string;
serviceStack?: string;
/**
* The id of a client/user request. Used to track a single request across services.
*/
xRequestId?: string;
/**
* String tags to help categorize and/or filter log events
*/
tags?: string[];
/**
* The current hostname for the service instance.
*/
host?: string;
/**
* The current port number for the service instance.
*/
port?: number;
/**
* A regular string or stringified JSON object containing data about log event.
*/
data?: string;
/**
* The URL from which the log event was initiated.
*/
url?: string;
/**
* The number of milliseconds spent on request/process.
*/
responseTime?: number;
/**
* HTTP status code if applicable.
*/
statusCode?: number;
/**
* HTTP method if applicable.
*/
method?: string;
error?: {
/**
* A string representation of error type, e.g. NO_SUCH_VARIABLE_ERROR.
*/
type?: string;
/**
* Error code uniquely identifiying error. E.g. 4324.
*/
code: string;
/**
* Name of service where error originated if the error originated outside this service.
*/
serviceName?: string;
/**
* Stack trace for the error. Should be santizied of sensitive information if possible.
*/
stack?: string;
/**
* Any data that may be related to error in string format.
*/
data?: string;
/**
* Regular string or stringified json object of original error object
*/
originalErrorObject?: string;
};
request?: {
/**
* Request headers like Accept, If-Modified-Since, etc. Avoid logging sensitive headers!
*/
headers?: {
[k: string]: string;
};
/**
* Query parameters contained within URL. E.g. (?myvalue=foo)
*/
query?: {
[k: string]: string | string[];
};
/**
* Path parameters contained whithin URL. E.g. /resource/<pathparam>
*/
path?: {
[k: string]: string;
};
/**
* The request payload/body if applicable (POST form, json payload, etc).
*/
payload?: string;
};
response?: {
/**
* Response headers like eTag, Content-Length, etc. Avoid logging sensitive headers!
*/
headers?: {
[k: string]: string;
};
/**
* A string representation of the response body. Do not log large bodies!
*/
body?: string;
};
}
declare module "log-schema.json" {
const value: LogSchema;
export default value;
}
\ No newline at end of file
import { LogSchema } from "log-schema-node";
import * as schema from "../log-schema/log-schema.json";
const minimalEvent: LogSchema = {
schemaVersion: "v3",
"@timestamp": "1985-04-12T23:20:50.52Z",
levelName: "INFO",
message: "Ørliten melding",
serviceName: "some-service",
serviceVersion: "v1.0.0"
};
const fullEvent: LogSchema = {
schemaVersion: "v3",
"@timestamp": "1985-04-12T23:20:50.52Z",
levelName: "INFO",
message: "Ørliten melding",
userId: "some-user-id",
process: "main process id",
thread: "main thread id",
file: "main.java",
line: 42,
loggerName: "some-logger",
serviceName: "some-service",
serviceVersion: "v1.0.0",
serviceStack: "raird-stage-1",
host: "somehost",
port: 2100,
tags: [
"performance",
],
url: "/v0/hello",
responseTime: 120,
statusCode: 200,
method: "GET",
xRequestId: "cd88dba8-c8e1-4b2e-8a19-bc6220e0be65",
request: {
headers: {
"User-Agent": "curl/7.47.0",
Accept: "*/*"
},
path: {
param1: "param1-value",
param2: "param2-value"
},
query: {
query1: "query1-value",
query2: ["query2-value1", "query2-value2"]
},
payload: "{\"command\": \"tabulate\"}"
},
response: {
headers: {
"Content-Type": "application/octet-stream",
"X-Request-ID": "cd88dba8-c8e1-4b2e-8a19-bc6220e0be65",
"Content-Disposition": "attachment; filename=\"hello.bin\""
},
body: "{\"message\": \"Hello world!\"}"
},
error: {
type: "some-error",
code: "some-error-123",
serviceName: "some-external-service",
stack: "stacktrace1\nstacktrace2\nstacktrace3",
data: "more details...",
originalErrorObject: "stringobject"
},
data: "some-extra-string-data"
};
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
// If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
// If the library is global (cannot be imported via `import` or `require`), leave this out.
"baseUrl": ".",
"paths": { "log-schema-node": ["."] }
}
}
\ No newline at end of file
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"indent": [true, "tabs"]
}
}
\ No newline at end of file
declare module "*.json" {
const value: any;
export default value;
}
......@@ -433,7 +433,7 @@ tslib@^1.6.0:
version "1.7.0"
resolved "https://nexus.nsd.no/repository/npm-group/tslib/-/tslib-1.7.0.tgz#6e8366695f72961252b35167b0dd4fbeeafba491"
tslint@^5.1.0:
tslint@^5.1.0, tslint@^5.2.0:
version "5.2.0"
resolved "https://nexus.nsd.no/repository/npm-group/tslint/-/tslint-5.2.0.tgz#16a2addf20cb748385f544e9a0edab086bc34114"
dependencies:
......@@ -452,6 +452,10 @@ tsutils@^1.1.0, tsutils@^1.8.0:
version "1.8.0"
resolved "https://nexus.nsd.no/repository/npm-group/tsutils/-/tsutils-1.8.0.tgz#bf8118ed8e80cd5c9fc7d75728c7963d44ed2f52"
typescript@^2.3.2:
version "2.3.2"
resolved "https://nexus.nsd.no/repository/npm-group/typescript/-/typescript-2.3.2.tgz#f0f045e196f69a72f06b25fd3bd39d01c3ce9984"
validator@^6.0.0:
version "6.3.0"
resolved "https://nexus.nsd.no/repository/npm-group/validator/-/validator-6.3.0.tgz#47ce23ed8d4eaddfa9d4b8ef0071b6cf1078d7c8"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment