@risemaxi/api-client
Version:
Client Library for Rise
208 lines • 9.29 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _RiseApiClient_instances, _RiseApiClient_baseUrl, _RiseApiClient_enabledParsing, _RiseApiClient_parse, _RiseApiClient_parseAsync, _RiseApiClient_constructPath, _RiseApiClient_request, _RiseApiHooks_client;
import { Value } from "@sinclair/typebox/value";
import { useInfiniteQuery, useMutation, useQuery, useQueryClient, } from "@tanstack/react-query";
import { EndpointByMethod, } from "./contract.js";
export class RiseApiClient {
constructor(fetcher) {
_RiseApiClient_instances.add(this);
this.fetcher = fetcher;
_RiseApiClient_baseUrl.set(this, "");
_RiseApiClient_enabledParsing.set(this, true);
}
setBaseUrl(baseUrl) {
__classPrivateFieldSet(this, _RiseApiClient_baseUrl, baseUrl, "f");
return this;
}
setEnableParsing(enable) {
__classPrivateFieldSet(this, _RiseApiClient_enabledParsing, enable, "f");
return this;
}
get(path, ...params) {
return __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_request).call(this, "get", path, ...params);
}
post(path, ...params) {
return __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_request).call(this, "post", path, ...params);
}
patch(path, ...params) {
return __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_request).call(this, "patch", path, ...params);
}
delete(path, ...params) {
return __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_request).call(this, "delete", path, ...params);
}
put(path, ...params) {
return __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_request).call(this, "put", path, ...params);
}
}
_RiseApiClient_baseUrl = new WeakMap(), _RiseApiClient_enabledParsing = new WeakMap(), _RiseApiClient_instances = new WeakSet(), _RiseApiClient_parse = function _RiseApiClient_parse(schema, value) {
return __classPrivateFieldGet(this, _RiseApiClient_enabledParsing, "f") ? Value.Parse(schema, value) : value;
}, _RiseApiClient_parseAsync = function _RiseApiClient_parseAsync(schema, value) {
return value.then((res) => __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_parse).call(this, schema, res));
}, _RiseApiClient_constructPath = function _RiseApiClient_constructPath(template, params) {
if (!params) {
return template;
}
return template.replace(/{(\w+)}/g, (match, key) => {
if (key in params) {
return params[key];
}
return match;
});
}, _RiseApiClient_request = function _RiseApiClient_request(method, path, ...params) {
const parameters = params[0];
const finalPath = __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_constructPath).call(this, path, parameters?.path);
const EndpointSchema = EndpointByMethod[method][path].properties;
return __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_parseAsync).call(this, EndpointSchema.response, this.fetcher(method, __classPrivateFieldGet(this, _RiseApiClient_baseUrl, "f") + finalPath, __classPrivateFieldGet(this, _RiseApiClient_instances, "m", _RiseApiClient_parse).call(this, EndpointSchema.parameters, parameters)));
};
export function createRiseApiClient(fetcher) {
return new RiseApiClient(fetcher);
}
export class RiseApiHooks {
constructor(client) {
_RiseApiHooks_client.set(this, void 0);
__classPrivateFieldSet(this, _RiseApiHooks_client, client, "f");
}
getCacheKey(method, path,
// @ts-expect-error cannot seem to index with parameters
...params) {
const key = `${method}_${path}`;
return [key, ...params];
}
setCachedData(queryClient, method, path, ...options) {
const [data, ...params] = options;
const queryKey = this.getCacheKey(method, path, ...params);
queryClient.setQueryData(queryKey, data);
}
useSetCachedData(method, path, ...options) {
const queryClient = useQueryClient();
this.setCachedData(queryClient, method, path, ...options);
}
getCachedData(queryClient, method, path,
// @ts-expect-error cannot seem to index with parameters
...params) {
const queryKey = this.getCacheKey(method, path, ...params);
return queryClient.getQueryData(queryKey);
}
useGetCachedData(method, path,
// @ts-expect-error cannot seem to index with parameters
...params) {
const queryClient = useQueryClient();
return this.getCachedData(queryClient, method, path, ...params);
}
prefetchData(queryClient, path, ...rest) {
const [config, options] = rest;
const queryKey = this.getCacheKey("get", path, config);
const queryFn = () => __classPrivateFieldGet(this, _RiseApiHooks_client, "f").get(path, config);
queryClient.prefetchQuery({
...options,
queryKey,
queryFn,
});
}
usePrefetchData(path, ...rest) {
const queryClient = useQueryClient();
this.prefetchData(queryClient, path, ...rest);
}
useGet(path, ...rest) {
const queryClient = useQueryClient();
const [config, options] = rest;
const queryKey = this.getCacheKey("get", path, config);
const invalidate = () => queryClient.invalidateQueries({
exact: true,
queryKey,
});
return {
...useQuery({
queryFn: () => __classPrivateFieldGet(this, _RiseApiHooks_client, "f").get(path, config),
queryKey,
...options,
}),
invalidate,
queryKey,
};
}
useInfiniteGet(path, configMapper, options) {
const queryClient = useQueryClient();
const queryKey = this.getCacheKey("get", path, configMapper({
meta: {},
queryKey: [],
signal: new AbortController().signal,
client: queryClient,
}));
const queryFn = (context) => {
const config = configMapper(context);
return __classPrivateFieldGet(this, _RiseApiHooks_client, "f").get(path, config);
};
const invalidate = () => queryClient.invalidateQueries({
queryKey,
});
return {
...useInfiniteQuery({
queryFn,
queryKey,
...options,
}),
invalidate,
queryKey,
};
}
usePost(path, options) {
const mutationKey = this.getCacheKey("post", path, {});
return {
...useMutation({
mutationFn: (params) => __classPrivateFieldGet(this, _RiseApiHooks_client, "f").post(path, params),
mutationKey,
...options,
}),
mutationKey,
};
}
usePatch(path, options) {
const mutationKey = this.getCacheKey("patch", path, {});
return {
...useMutation({
mutationFn: (params) => __classPrivateFieldGet(this, _RiseApiHooks_client, "f").patch(path, params),
mutationKey,
...options,
}),
mutationKey,
};
}
useDelete(path, options) {
const mutationKey = this.getCacheKey("delete", path, {});
return {
...useMutation({
mutationFn: (params) => __classPrivateFieldGet(this, _RiseApiHooks_client, "f").delete(path, params),
mutationKey,
...options,
}),
mutationKey,
};
}
usePut(path, options) {
const mutationKey = this.getCacheKey("put", path, {});
return {
...useMutation({
mutationFn: (params) => __classPrivateFieldGet(this, _RiseApiHooks_client, "f").put(path, params),
mutationKey,
...options,
}),
mutationKey,
};
}
}
_RiseApiHooks_client = new WeakMap();
export function createRiseApiHooks(client) {
return new RiseApiHooks(client);
}
//# sourceMappingURL=client.js.map