tag-rpc 1.0.1 → 1.0.3

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.ts CHANGED
@@ -1,30 +1,8 @@
1
- type BaseApiRoutes = Record<string, {
2
- PathParams: any;
3
- Methods: Record<string, {
4
- Response: any;
5
- Body: any;
6
- Query: any;
7
- }>;
8
- }>;
9
- interface BaseNamedRoutes {
10
- [key: string]: {
11
- path: string;
12
- method: string;
13
- };
14
- }
15
- type TagRPCOptions<T extends BaseApiRoutes, TPath extends keyof T, TMethod extends keyof T[TPath]["Methods"]> = Omit<RequestInit, "method" | "body"> & (T[TPath]["PathParams"] extends never ? {
16
- pathParams?: never;
17
- } : {
18
- pathParams: T[TPath]["PathParams"];
19
- }) & (T[TPath]["Methods"][TMethod]["Body"] extends never ? {
20
- body?: never;
21
- } : {
22
- body: T[TPath]["Methods"][TMethod]["Body"];
23
- }) & (T[TPath]["Methods"][TMethod]["Query"] extends never ? {
24
- query?: never;
25
- } : {
26
- query?: T[TPath]["Methods"][TMethod]["Query"];
27
- });
1
+ type MethodShape = {
2
+ Response: unknown;
3
+ Body: unknown;
4
+ Query: unknown;
5
+ };
28
6
  declare class TAG_API_ERROR extends Error {
29
7
  status: number;
30
8
  statusText: string;
@@ -32,10 +10,55 @@ declare class TAG_API_ERROR extends Error {
32
10
  url: string;
33
11
  constructor(status: number, statusText: string, body: any, url: string);
34
12
  }
35
- declare function createTagRPC<T extends BaseApiRoutes, N extends BaseNamedRoutes>(namedRoutes: N): { [K in keyof N]: (options: TagRPCOptions<T, N[K]["path"] & keyof T, N[K]["method"] & keyof T[N[K]["path"] & keyof T]["Methods"]>) => Promise<T[N[K]["path"] & keyof T]["Methods"][N[K]["method"] & keyof T[N[K]["path"] & keyof T]["Methods"]]["Response"]>; } & {
36
- fetch: <P extends keyof T, M extends keyof T[P]["Methods"] & string>(path: P, method: M, options: TagRPCOptions<T, P, M>) => Promise<T[P]["Methods"][M]["Response"]>;
37
- call: <K_1 extends keyof N>(route: N[K_1], options: TagRPCOptions<T, N[K_1]["path"] & keyof T, N[K_1]["method"] & keyof T[N[K_1]["path"] & keyof T]["Methods"]>) => Promise<T[N[K_1]["path"] & keyof T]["Methods"][N[K_1]["method"] & keyof T[N[K_1]["path"] & keyof T]["Methods"]]["Response"]>;
38
- routes: N;
13
+ declare function createTagRPC<TApiRoutes extends Record<string, {
14
+ Methods: Record<string, MethodShape>;
15
+ PathParams: any;
16
+ }>, TNamedRoutes extends Record<string, {
17
+ path: keyof TApiRoutes;
18
+ method: string;
19
+ }>>(registry: {
20
+ ApiRoutes: TApiRoutes;
21
+ namedRoutes: TNamedRoutes;
22
+ }): { [K in keyof TNamedRoutes]: <TPath extends TNamedRoutes[K]["path"] & keyof TApiRoutes, TMethod extends TNamedRoutes[K]["method"] & keyof TApiRoutes[TPath]["Methods"] & string>(options: Omit<RequestInit, "method" | "body"> & (TApiRoutes[TPath]["PathParams"] extends never ? {
23
+ pathParams?: never;
24
+ } : {
25
+ pathParams: TApiRoutes[TPath]["PathParams"];
26
+ }) & (TApiRoutes[TPath]["Methods"][TMethod]["Body"] extends never ? {
27
+ body?: never;
28
+ } : {
29
+ body: TApiRoutes[TPath]["Methods"][TMethod]["Body"];
30
+ }) & (TApiRoutes[TPath]["Methods"][TMethod]["Query"] extends never ? {
31
+ query?: never;
32
+ } : {
33
+ query?: TApiRoutes[TPath]["Methods"][TMethod]["Query"] | undefined;
34
+ })) => Promise<TApiRoutes[TPath]["Methods"][TMethod]["Response"]>; } & {
35
+ fetch: <TPath extends keyof TApiRoutes, TMethod extends keyof TApiRoutes[TPath]["Methods"] & string>(path: TPath, method: TMethod, options: Omit<RequestInit, "method" | "body"> & (TApiRoutes[TPath]["PathParams"] extends never ? {
36
+ pathParams?: never;
37
+ } : {
38
+ pathParams: TApiRoutes[TPath]["PathParams"];
39
+ }) & (TApiRoutes[TPath]["Methods"][TMethod]["Body"] extends never ? {
40
+ body?: never;
41
+ } : {
42
+ body: TApiRoutes[TPath]["Methods"][TMethod]["Body"];
43
+ }) & (TApiRoutes[TPath]["Methods"][TMethod]["Query"] extends never ? {
44
+ query?: never;
45
+ } : {
46
+ query?: TApiRoutes[TPath]["Methods"][TMethod]["Query"] | undefined;
47
+ })) => Promise<TApiRoutes[TPath]["Methods"][TMethod]["Response"]>;
48
+ routes: TNamedRoutes;
49
+ call: <K_1 extends keyof TNamedRoutes, TRoute extends TNamedRoutes[K_1], TPath extends TRoute["path"] & keyof TApiRoutes, TMethod extends TRoute["method"] & keyof TApiRoutes[TPath]["Methods"] & string>(route: TRoute, options: Omit<RequestInit, "method" | "body"> & (TApiRoutes[TPath]["PathParams"] extends never ? {
50
+ pathParams?: never;
51
+ } : {
52
+ pathParams: TApiRoutes[TPath]["PathParams"];
53
+ }) & (TApiRoutes[TPath]["Methods"][TMethod]["Body"] extends never ? {
54
+ body?: never;
55
+ } : {
56
+ body: TApiRoutes[TPath]["Methods"][TMethod]["Body"];
57
+ }) & (TApiRoutes[TPath]["Methods"][TMethod]["Query"] extends never ? {
58
+ query?: never;
59
+ } : {
60
+ query?: TApiRoutes[TPath]["Methods"][TMethod]["Query"] | undefined;
61
+ })) => Promise<TApiRoutes[TPath]["Methods"][TMethod]["Response"]>;
39
62
  };
40
63
 
41
- export { type BaseApiRoutes, type BaseNamedRoutes, TAG_API_ERROR, type TagRPCOptions, createTagRPC };
64
+ export { TAG_API_ERROR, createTagRPC };
package/dist/index.js CHANGED
@@ -6,47 +6,54 @@ var TAG_API_ERROR = class extends Error {
6
6
  this.statusText = statusText;
7
7
  this.body = body;
8
8
  this.url = url;
9
- this.name = "TagApiError";
9
+ this.name = "ApiError";
10
10
  }
11
11
  };
12
- async function fetchData(path, method, options) {
13
- let url = path;
14
- if (options?.pathParams) {
15
- for (const [key, value] of Object.entries(options.pathParams)) {
16
- url = url.replace(`[${key}]`, encodeURIComponent(String(value)));
12
+ function createTagRPC(registry) {
13
+ async function fetchData(path, method, options) {
14
+ let url = path;
15
+ if ("pathParams" in options && options.pathParams) {
16
+ for (const [key, value] of Object.entries(options.pathParams)) {
17
+ url = url.replace(`[${key}]`, encodeURIComponent(String(value)));
18
+ }
17
19
  }
18
- }
19
- if (options?.query) {
20
- const qs = new URLSearchParams(options.query).toString();
21
- if (qs) url += (url.includes("?") ? "&" : "?") + qs;
22
- }
23
- const res = await fetch(url, {
24
- ...options,
25
- method,
26
- body: options?.body ? JSON.stringify(options.body) : void 0,
27
- headers: { "Content-Type": "application/json", ...options?.headers }
28
- });
29
- if (!res.ok) {
30
- let errorBody;
31
- try {
32
- errorBody = await res.json();
33
- } catch {
34
- errorBody = { message: "Unknown error occurred" };
20
+ if ("query" in options) {
21
+ const qs = new URLSearchParams(
22
+ options.query
23
+ ).toString();
24
+ if (qs) url += `?${qs}`;
35
25
  }
36
- throw new TAG_API_ERROR(res.status, res.statusText, errorBody, url);
26
+ const res = await fetch(url, {
27
+ ...options,
28
+ method,
29
+ body: "body" in options ? JSON.stringify(options.body) : void 0,
30
+ headers: {
31
+ "Content-Type": "application/json",
32
+ ...options.headers
33
+ }
34
+ });
35
+ if (!res.ok) {
36
+ let errorBody;
37
+ try {
38
+ errorBody = await res.json();
39
+ } catch {
40
+ errorBody = { message: "Unknown error occurred" };
41
+ }
42
+ throw new TAG_API_ERROR(res.status, res.statusText, errorBody, url);
43
+ }
44
+ return res.json();
37
45
  }
38
- return res.json();
39
- }
40
- function createTagRPC(namedRoutes) {
41
46
  const baseFetcher = {
42
- fetch: (path, method, options) => fetchData(path, method, options),
43
- call: (route, options) => fetchData(route.path, route.method, options),
44
- routes: namedRoutes
47
+ fetch: fetchData,
48
+ routes: registry.namedRoutes,
49
+ call: (route, options) => fetchData(route.path, route.method, options)
45
50
  };
46
51
  return new Proxy(baseFetcher, {
47
52
  get(target, prop) {
48
- if (prop in target) return target[prop];
49
- const route = namedRoutes[prop];
53
+ if (prop in target) {
54
+ return target[prop];
55
+ }
56
+ const route = registry.namedRoutes[prop];
50
57
  if (route) {
51
58
  return (options) => fetchData(route.path, route.method, options);
52
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tag-rpc",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "End-to-end type safety for Next.js Route Handlers",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",