@hiroi/create-api 0.0.1
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/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +73 -0
- package/dist/index.mjs +36 -0
- package/package.json +34 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface Params<B> {
|
|
2
|
+
path?: Record<string, string | number | boolean>;
|
|
3
|
+
query?: Record<string, string | number | boolean>;
|
|
4
|
+
body?: B;
|
|
5
|
+
}
|
|
6
|
+
declare function fetcher<P extends Params<B>, R, B extends Record<string, any>>(method: 'get' | 'post' | 'patch' | 'delete', path: string, params: P, init?: Omit<RequestInit, 'method' | 'body'>, apiUriBase?: string): Promise<R>;
|
|
7
|
+
|
|
8
|
+
declare function createApi<P extends Params<B>, R, B extends Record<string, any> = never>(method: 'get' | 'post' | 'patch' | 'delete', path: string, apiUriBase?: string): ((params: P, init?: Parameters<typeof fetcher<P, R, B>>[3]) => Promise<R>) & {
|
|
9
|
+
queryKey: (params: P) => unknown[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { createApi };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface Params<B> {
|
|
2
|
+
path?: Record<string, string | number | boolean>;
|
|
3
|
+
query?: Record<string, string | number | boolean>;
|
|
4
|
+
body?: B;
|
|
5
|
+
}
|
|
6
|
+
declare function fetcher<P extends Params<B>, R, B extends Record<string, any>>(method: 'get' | 'post' | 'patch' | 'delete', path: string, params: P, init?: Omit<RequestInit, 'method' | 'body'>, apiUriBase?: string): Promise<R>;
|
|
7
|
+
|
|
8
|
+
declare function createApi<P extends Params<B>, R, B extends Record<string, any> = never>(method: 'get' | 'post' | 'patch' | 'delete', path: string, apiUriBase?: string): ((params: P, init?: Parameters<typeof fetcher<P, R, B>>[3]) => Promise<R>) & {
|
|
9
|
+
queryKey: (params: P) => unknown[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { createApi };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createApi: () => createApi
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/utils.ts
|
|
38
|
+
var import_querystring = __toESM(require("querystring"));
|
|
39
|
+
var MethodMap = {
|
|
40
|
+
get: "GET",
|
|
41
|
+
post: "POST",
|
|
42
|
+
patch: "PATCH",
|
|
43
|
+
delete: "DELETE"
|
|
44
|
+
};
|
|
45
|
+
async function fetcher(method, path, params, init, apiUriBase = "") {
|
|
46
|
+
const url = `${apiUriBase}${interpolatePath(`${path}?${import_querystring.default.stringify(params?.query)}`, params?.path)}`;
|
|
47
|
+
const response = await fetch(url, {
|
|
48
|
+
...init,
|
|
49
|
+
method: MethodMap[method],
|
|
50
|
+
body: JSON.stringify(params?.body)
|
|
51
|
+
});
|
|
52
|
+
const data = await response.json();
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
function interpolatePath(path, pathParams) {
|
|
56
|
+
return path.replace(/{(\w+)}/g, (_, key) => encodeURIComponent(pathParams?.[key] || "undefined"));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/index.ts
|
|
60
|
+
function createApi(method, path, apiUriBase = "") {
|
|
61
|
+
const queryKey = (params) => queryKeyGen(method, path, params, apiUriBase);
|
|
62
|
+
return Object.assign(
|
|
63
|
+
(params, init) => fetcher(method, path, params, init, apiUriBase),
|
|
64
|
+
{ queryKey: (params) => queryKey(params) }
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
function queryKeyGen(method, path, params, apiUriBase = "") {
|
|
68
|
+
return [method, `${apiUriBase}${path}`, { path: params.path, query: params.query }];
|
|
69
|
+
}
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
createApi
|
|
73
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
import qs from "querystring";
|
|
3
|
+
var MethodMap = {
|
|
4
|
+
get: "GET",
|
|
5
|
+
post: "POST",
|
|
6
|
+
patch: "PATCH",
|
|
7
|
+
delete: "DELETE"
|
|
8
|
+
};
|
|
9
|
+
async function fetcher(method, path, params, init, apiUriBase = "") {
|
|
10
|
+
const url = `${apiUriBase}${interpolatePath(`${path}?${qs.stringify(params?.query)}`, params?.path)}`;
|
|
11
|
+
const response = await fetch(url, {
|
|
12
|
+
...init,
|
|
13
|
+
method: MethodMap[method],
|
|
14
|
+
body: JSON.stringify(params?.body)
|
|
15
|
+
});
|
|
16
|
+
const data = await response.json();
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
function interpolatePath(path, pathParams) {
|
|
20
|
+
return path.replace(/{(\w+)}/g, (_, key) => encodeURIComponent(pathParams?.[key] || "undefined"));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
function createApi(method, path, apiUriBase = "") {
|
|
25
|
+
const queryKey = (params) => queryKeyGen(method, path, params, apiUriBase);
|
|
26
|
+
return Object.assign(
|
|
27
|
+
(params, init) => fetcher(method, path, params, init, apiUriBase),
|
|
28
|
+
{ queryKey: (params) => queryKey(params) }
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function queryKeyGen(method, path, params, apiUriBase = "") {
|
|
32
|
+
return [method, `${apiUriBase}${path}`, { path: params.path, query: params.query }];
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
createApi
|
|
36
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hiroi/create-api",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"source": "./src/index.ts",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "^25.2.3",
|
|
15
|
+
"@hiroi/typescript-config": "0.0.1"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
19
|
+
"clean": "rm -rf dist"
|
|
20
|
+
},
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|