tag-rpc 1.0.0 → 1.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.ts CHANGED
@@ -1,28 +1,17 @@
1
- /**
2
- * Base types that describe the shape of the generated registry.
3
- * This allows the package to compile without knowing the exact routes yet.
4
- */
5
- interface BaseApiRoutes {
6
- [path: string]: {
7
- PathParams: any;
8
- Methods: {
9
- [method: string]: {
10
- Response: any;
11
- Body: any;
12
- Query: any;
13
- };
14
- };
15
- };
16
- }
1
+ type BaseApiRoutes = Record<string, {
2
+ PathParams: any;
3
+ Methods: Record<string, {
4
+ Response: any;
5
+ Body: any;
6
+ Query: any;
7
+ }>;
8
+ }>;
17
9
  interface BaseNamedRoutes {
18
10
  [key: string]: {
19
11
  path: string;
20
12
  method: string;
21
13
  };
22
14
  }
23
- /**
24
- * Types for the fetcher options, extracted to be reusable.
25
- */
26
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 ? {
27
16
  pathParams?: never;
28
17
  } : {
@@ -36,9 +25,6 @@ type TagRPCOptions<T extends BaseApiRoutes, TPath extends keyof T, TMethod exten
36
25
  } : {
37
26
  query?: T[TPath]["Methods"][TMethod]["Query"];
38
27
  });
39
- /**
40
- * Custom Error Class for API failures
41
- */
42
28
  declare class TAG_API_ERROR extends Error {
43
29
  status: number;
44
30
  statusText: string;
@@ -46,12 +32,9 @@ declare class TAG_API_ERROR extends Error {
46
32
  url: string;
47
33
  constructor(status: number, statusText: string, body: any, url: string);
48
34
  }
49
- /**
50
- * THE MAIN EXPORT: This creates the proxy-based SDK.
51
- * Users will call this and pass in their generated types/objects.
52
- */
53
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"]>; } & {
54
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"]>;
55
38
  routes: N;
56
39
  };
57
40
 
package/dist/index.js CHANGED
@@ -11,20 +11,20 @@ var TAG_API_ERROR = class extends Error {
11
11
  };
12
12
  async function fetchData(path, method, options) {
13
13
  let url = path;
14
- if (options.pathParams) {
14
+ if (options?.pathParams) {
15
15
  for (const [key, value] of Object.entries(options.pathParams)) {
16
16
  url = url.replace(`[${key}]`, encodeURIComponent(String(value)));
17
17
  }
18
18
  }
19
- if (options.query) {
19
+ if (options?.query) {
20
20
  const qs = new URLSearchParams(options.query).toString();
21
- if (qs) url += `?${qs}`;
21
+ if (qs) url += (url.includes("?") ? "&" : "?") + qs;
22
22
  }
23
23
  const res = await fetch(url, {
24
24
  ...options,
25
25
  method,
26
- body: options.body ? JSON.stringify(options.body) : void 0,
27
- headers: { "Content-Type": "application/json", ...options.headers }
26
+ body: options?.body ? JSON.stringify(options.body) : void 0,
27
+ headers: { "Content-Type": "application/json", ...options?.headers }
28
28
  });
29
29
  if (!res.ok) {
30
30
  let errorBody;
@@ -40,6 +40,7 @@ async function fetchData(path, method, options) {
40
40
  function createTagRPC(namedRoutes) {
41
41
  const baseFetcher = {
42
42
  fetch: (path, method, options) => fetchData(path, method, options),
43
+ call: (route, options) => fetchData(route.path, route.method, options),
43
44
  routes: namedRoutes
44
45
  };
45
46
  return new Proxy(baseFetcher, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tag-rpc",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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",