next-rest-framework
Version:
Next REST Framework - Type-safe, self-documenting APIs for Next.js
71 lines (68 loc) • 1.85 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/client/index.ts
var client_exports = {};
__export(client_exports, {
rpcClient: () => rpcClient
});
module.exports = __toCommonJS(client_exports);
// src/client/rpc-client.ts
var fetcher = async ({
url,
body,
init
}) => {
const opts = {
...init,
method: "POST",
headers: {
...init?.headers,
"Content-Type": "application/json"
}
};
if (body) {
opts.body = JSON.stringify(body);
}
const res = await fetch(url, opts);
if (!res.ok) {
const error = await res.json();
throw new Error(error);
}
return await res.json();
};
var rpcClient = ({
url: _url,
init
}) => {
return new Proxy({}, {
get: (_, prop) => {
if (typeof prop === "string") {
return async (body) => {
const baseUrl = _url.endsWith("/") ? _url : `${_url}/`;
const url = `${baseUrl}${prop}`;
return await fetcher({ url, body, init });
};
}
}
});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rpcClient
});
;