@neupgroup/mapper 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.
- package/README.md +66 -0
- package/api.d.ts +22 -0
- package/api.js +14 -0
- package/db.d.ts +19 -0
- package/db.js +15 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Neup.Mapper (Library)
|
|
2
|
+
|
|
3
|
+
Neup.Mapper is now a small, framework-agnostic TypeScript library focused on two things:
|
|
4
|
+
|
|
5
|
+
- Database connectivity via adapter pattern (bring your own driver)
|
|
6
|
+
- HTTP API access via adapter pattern (bring your own client)
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
This project is currently private. Build locally:
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
npm install
|
|
14
|
+
npm run build
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Artifacts are emitted to `dist/`.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Database
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
import { createDb, IDbAdapter } from "neup-mapper";
|
|
25
|
+
|
|
26
|
+
const adapter: IDbAdapter = {
|
|
27
|
+
async connect(config) {
|
|
28
|
+
// initialize your driver, e.g., pg, mysql2, sqlite, etc.
|
|
29
|
+
},
|
|
30
|
+
async query(sql, params) {
|
|
31
|
+
// delegate to your driver
|
|
32
|
+
return [];
|
|
33
|
+
},
|
|
34
|
+
async close() {
|
|
35
|
+
// close driver resources
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const db = createDb(adapter);
|
|
40
|
+
await db.connect({ host: "localhost" });
|
|
41
|
+
const rows = await db.query("SELECT 1");
|
|
42
|
+
await db.close();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### API
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
import { createApiClient, IApiAdapter } from "neup-mapper";
|
|
49
|
+
|
|
50
|
+
const http: IApiAdapter = {
|
|
51
|
+
async request(req) {
|
|
52
|
+
// use fetch/axios/undici, return { status, headers, data }
|
|
53
|
+
return { status: 200, data: { ok: true } };
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const api = createApiClient("https://api.example.com", http, { Authorization: "Bearer token" });
|
|
58
|
+
const res = await api.get("/users");
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Layout
|
|
62
|
+
|
|
63
|
+
- Source files live at the project root: `index.ts`, `db.ts`, `api.ts`.
|
|
64
|
+
- Build emits to `dist/` with `index.js` and `index.d.ts` for consumers.
|
|
65
|
+
- No React/Next.js UI is included; the project is library-only.
|
|
66
|
+
- Add any DB driver or HTTP client as dependencies in your own app.
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface ApiRequest {
|
|
2
|
+
method: string;
|
|
3
|
+
url: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
body?: unknown;
|
|
6
|
+
}
|
|
7
|
+
export interface ApiResponse<T = unknown> {
|
|
8
|
+
status: number;
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
data?: T;
|
|
11
|
+
}
|
|
12
|
+
export interface ApiAdapter {
|
|
13
|
+
request<T = unknown>(req: ApiRequest): Promise<ApiResponse<T>>;
|
|
14
|
+
}
|
|
15
|
+
export interface ApiClient {
|
|
16
|
+
get<T = unknown>(path: string, headers?: Record<string, string>): Promise<ApiResponse<T>>;
|
|
17
|
+
post<T = unknown>(path: string, body?: unknown, headers?: Record<string, string>): Promise<ApiResponse<T>>;
|
|
18
|
+
put<T = unknown>(path: string, body?: unknown, headers?: Record<string, string>): Promise<ApiResponse<T>>;
|
|
19
|
+
delete<T = unknown>(path: string, headers?: Record<string, string>): Promise<ApiResponse<T>>;
|
|
20
|
+
}
|
|
21
|
+
export declare function createApiClient(baseUrl: string, adapter: ApiAdapter, defaultHeaders?: Record<string, string>): ApiClient;
|
|
22
|
+
export type { ApiAdapter as IApiAdapter };
|
package/api.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function createApiClient(baseUrl, adapter, defaultHeaders) {
|
|
2
|
+
const buildUrl = (path) => {
|
|
3
|
+
if (!baseUrl)
|
|
4
|
+
return path;
|
|
5
|
+
return baseUrl.endsWith("/") ? `${baseUrl}${path.replace(/^\//, "")}` : `${baseUrl}/${path.replace(/^\//, "")}`;
|
|
6
|
+
};
|
|
7
|
+
const mergeHeaders = (headers) => ({ ...(defaultHeaders || {}), ...(headers || {}) });
|
|
8
|
+
return {
|
|
9
|
+
get: (path, headers) => adapter.request({ method: "GET", url: buildUrl(path), headers: mergeHeaders(headers) }),
|
|
10
|
+
post: (path, body, headers) => adapter.request({ method: "POST", url: buildUrl(path), headers: mergeHeaders(headers), body }),
|
|
11
|
+
put: (path, body, headers) => adapter.request({ method: "PUT", url: buildUrl(path), headers: mergeHeaders(headers), body }),
|
|
12
|
+
delete: (path, headers) => adapter.request({ method: "DELETE", url: buildUrl(path), headers: mergeHeaders(headers) }),
|
|
13
|
+
};
|
|
14
|
+
}
|
package/db.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type QueryParams = unknown[] | Record<string, unknown> | undefined;
|
|
2
|
+
export interface DbAdapter {
|
|
3
|
+
connect(config: Record<string, unknown>): Promise<void>;
|
|
4
|
+
query<T = unknown>(sql: string, params?: QueryParams): Promise<T[]>;
|
|
5
|
+
execute?(sql: string, params?: QueryParams): Promise<{
|
|
6
|
+
affectedRows?: number;
|
|
7
|
+
}>;
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export interface Db {
|
|
11
|
+
connect: (config: Record<string, unknown>) => Promise<void>;
|
|
12
|
+
query: <T = unknown>(sql: string, params?: QueryParams) => Promise<T[]>;
|
|
13
|
+
execute: (sql: string, params?: QueryParams) => Promise<{
|
|
14
|
+
affectedRows?: number;
|
|
15
|
+
}>;
|
|
16
|
+
close: () => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export declare function createDb(adapter: DbAdapter): Db;
|
|
19
|
+
export type { DbAdapter as IDbAdapter };
|
package/db.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function createDb(adapter) {
|
|
2
|
+
const ensure = (fn) => fn();
|
|
3
|
+
return {
|
|
4
|
+
connect: (config) => ensure(() => adapter.connect(config)),
|
|
5
|
+
query: (sql, params) => ensure(() => adapter.query(sql, params)),
|
|
6
|
+
execute: (sql, params) => ensure(async () => {
|
|
7
|
+
if (typeof adapter.execute === "function") {
|
|
8
|
+
return adapter.execute(sql, params);
|
|
9
|
+
}
|
|
10
|
+
await adapter.query(sql, params);
|
|
11
|
+
return { affectedRows: undefined };
|
|
12
|
+
}),
|
|
13
|
+
close: () => ensure(() => adapter.close()),
|
|
14
|
+
};
|
|
15
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neupgroup/mapper",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"files": ["*.js", "*.d.ts"],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json",
|
|
20
|
+
"typecheck": "tsc --noEmit"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5"
|
|
25
|
+
}
|
|
26
|
+
}
|