oniaz-logging-system-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +36 -0
  2. package/index.d.ts +32 -0
  3. package/index.js +105 -0
  4. package/package.json +28 -0
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Oniaz Logging System SDK
2
+
3
+ Use this package from a backend application to send logs to the Logging System API.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install oniaz-logging-system-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import loggingSdk from "oniaz-logging-system-sdk";
15
+
16
+ loggingSdk.init(
17
+ "your-api-key",
18
+ "your-application-name"
19
+ );
20
+
21
+ await loggingSdk.log({
22
+ message: "User signed in successfully",
23
+ level: "INFO",
24
+ });
25
+ ```
26
+
27
+ ## Notes
28
+
29
+ - `init(apiKey, applicationName)` stores the API key and application name for later requests.
30
+ - `log(data)` sends a `POST /api/applications/:name/logs` request with `message` and `level` in the body, plus the API key in the `x-api-key` header.
31
+ - The server validates that the API key owner also owns the application before accepting the log.
32
+ - The SDK always sends logs to `https://oniaz-logging-system-api.vercel.app`.
33
+
34
+ ## API Endpoint
35
+
36
+ This SDK sends logs to `https://oniaz-logging-system-api.vercel.app`.
package/index.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ export interface InitOptions {
2
+ fetchImpl?: typeof fetch;
3
+ }
4
+
5
+ export interface LogData {
6
+ message: string;
7
+ level: string;
8
+ }
9
+
10
+ export interface LogResponse {
11
+ message?: string;
12
+ log?: {
13
+ id: string;
14
+ applicationName: string;
15
+ message: string;
16
+ level: string;
17
+ count: number;
18
+ createdAt: string;
19
+ updatedAt: string;
20
+ };
21
+ }
22
+
23
+ export interface LogSdk {
24
+ init(apiKey: string, applicationName: string, options?: InitOptions): LogSdk;
25
+ log(logData: LogData): Promise<LogResponse | string | null>;
26
+ }
27
+
28
+ export declare const init: LogSdk["init"];
29
+ export declare const log: LogSdk["log"];
30
+
31
+ declare const api: LogSdk;
32
+ export default api;
package/index.js ADDED
@@ -0,0 +1,105 @@
1
+ const API_BASE_URL = "https://oniaz-logging-system-api.vercel.app";
2
+
3
+ const state = {
4
+ apiKey: null,
5
+ applicationName: null,
6
+ fetchImpl: globalThis.fetch?.bind(globalThis) || null,
7
+ };
8
+
9
+ function assertInitialized() {
10
+ if (!state.apiKey || !state.applicationName) {
11
+ throw new Error("SDK not initialized. Call init(apiKey, applicationName) before log().");
12
+ }
13
+
14
+ if (!state.fetchImpl) {
15
+ throw new Error("No fetch implementation available. Provide one through init(..., { fetchImpl }).");
16
+ }
17
+ }
18
+
19
+ function buildLogUrl() {
20
+ return new URL(`/api/applications/${encodeURIComponent(state.applicationName)}/logs`, API_BASE_URL).toString();
21
+ }
22
+
23
+ function parseResponseBody(bodyText) {
24
+ if (!bodyText) {
25
+ return null;
26
+ }
27
+
28
+ try {
29
+ return JSON.parse(bodyText);
30
+ } catch {
31
+ return bodyText;
32
+ }
33
+ }
34
+
35
+ export function init(apiKey, applicationName, options = {}) {
36
+ if (!apiKey || typeof apiKey !== "string") {
37
+ throw new Error("A valid apiKey is required.");
38
+ }
39
+
40
+ if (!applicationName || typeof applicationName !== "string") {
41
+ throw new Error("A valid applicationName is required.");
42
+ }
43
+
44
+ state.apiKey = apiKey.trim();
45
+ state.applicationName = applicationName.trim();
46
+ state.fetchImpl = options.fetchImpl || globalThis.fetch?.bind(globalThis) || null;
47
+
48
+ return api;
49
+ }
50
+
51
+ export async function log(logData) {
52
+ assertInitialized();
53
+
54
+ if (!logData || typeof logData !== "object" || Array.isArray(logData)) {
55
+ throw new Error("log() expects a log data object.");
56
+ }
57
+
58
+ const payload = { ...logData };
59
+ const allowedKeys = ["message", "level"];
60
+
61
+ for (const key of Object.keys(payload)) {
62
+ if (!allowedKeys.includes(key)) {
63
+ throw new Error("log() only accepts message and level.");
64
+ }
65
+ }
66
+
67
+ if (typeof payload.message !== "string" || !payload.message.trim()) {
68
+ throw new Error("log() requires a non-empty message.");
69
+ }
70
+
71
+ if (typeof payload.level !== "string" || !payload.level.trim()) {
72
+ throw new Error("log() requires a non-empty level.");
73
+ }
74
+
75
+ payload.message = payload.message.trim();
76
+ payload.level = payload.level.trim().toUpperCase();
77
+
78
+ const response = await state.fetchImpl(buildLogUrl(), {
79
+ method: "POST",
80
+ headers: {
81
+ "Content-Type": "application/json",
82
+ "x-api-key": state.apiKey,
83
+ },
84
+ body: JSON.stringify(payload),
85
+ });
86
+
87
+ const bodyText = await response.text();
88
+ const body = parseResponseBody(bodyText);
89
+
90
+ if (!response.ok) {
91
+ const error = new Error(body?.message || `Log request failed with status ${response.status}.`);
92
+ error.status = response.status;
93
+ error.response = body;
94
+ throw error;
95
+ }
96
+
97
+ return body;
98
+ }
99
+
100
+ const api = {
101
+ init,
102
+ log,
103
+ };
104
+
105
+ export default api;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "oniaz-logging-system-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Server SDK for sending logs to the Logging System API.",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "exports": {
8
+ ".": "./index.js"
9
+ },
10
+ "files": [
11
+ "index.js",
12
+ "index.d.ts",
13
+ "README.md"
14
+ ],
15
+ "engines": {
16
+ "node": ">=18"
17
+ },
18
+ "keywords": [
19
+ "logging",
20
+ "sdk",
21
+ "server",
22
+ "api"
23
+ ],
24
+ "license": "ISC",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ }
28
+ }