@arrirpc/client 0.46.0 → 0.48.0

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.cjs CHANGED
@@ -71,6 +71,17 @@ class ArriErrorInstance extends Error {
71
71
  }
72
72
  }
73
73
 
74
+ async function getHeaders(input) {
75
+ if (typeof input === "function") {
76
+ const result = input();
77
+ if ("then" in result && typeof result.then === "function") {
78
+ return result.then((data) => data);
79
+ }
80
+ return result;
81
+ }
82
+ return input;
83
+ }
84
+
74
85
  async function arriRequest(opts) {
75
86
  let url = opts.url;
76
87
  let body;
@@ -94,12 +105,7 @@ async function arriRequest(opts) {
94
105
  break;
95
106
  }
96
107
  try {
97
- let headers = {};
98
- if (typeof opts.headers === "function") {
99
- headers = opts.headers();
100
- } else {
101
- headers = opts.headers ?? {};
102
- }
108
+ const headers = await getHeaders(opts.headers) ?? {};
103
109
  if (contentType)
104
110
  headers["Content-Type"] = contentType;
105
111
  if (opts.clientVersion)
@@ -182,13 +188,8 @@ function arriSseRequest(opts, options) {
182
188
  }
183
189
  const eventSource = new eventSourcePlus.EventSourcePlus(url, {
184
190
  method: opts.method ?? "get",
185
- headers: () => {
186
- let headers;
187
- if (typeof opts.headers === "function") {
188
- headers = opts.headers();
189
- } else {
190
- headers = opts.headers ?? {};
191
- }
191
+ headers: async () => {
192
+ const headers = await getHeaders(opts.headers) ?? {};
192
193
  if (opts.clientVersion) {
193
194
  headers["client-version"] = opts.clientVersion;
194
195
  }
@@ -309,14 +310,9 @@ function connectWebsocket(url, protocol) {
309
310
  protocol
310
311
  });
311
312
  }
312
- function arriWsRequest(opts, retryCount = 0) {
313
+ async function arriWsRequest(opts, retryCount = 0) {
313
314
  let url = opts.url.replace("http://", "ws://").replace("https://", "wss://");
314
- let headers;
315
- if (typeof opts.headers === "function") {
316
- headers = opts.headers();
317
- } else {
318
- headers = opts.headers;
319
- }
315
+ const headers = await getHeaders(opts.headers);
320
316
  if (headers) {
321
317
  if (opts.clientVersion)
322
318
  headers["client-version"] = opts.clientVersion;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { HttpMethod, OnRequestContext, OnRequestErrorContext, OnResponseContext, OnResponseErrorContext, EventSourceController } from 'event-source-plus';
1
+ import { HttpMethod, EventSourcePlusOptions, OnRequestContext, OnRequestErrorContext, OnResponseContext, OnResponseErrorContext, EventSourceController } from 'event-source-plus';
2
2
  export * from 'event-source-plus';
3
3
 
4
4
  interface ArriError {
@@ -32,7 +32,7 @@ declare class ArriErrorInstance extends Error implements ArriError {
32
32
  interface ArriRequestOpts<TType, TParams extends Record<any, any> | undefined = undefined> {
33
33
  url: string;
34
34
  method: HttpMethod;
35
- headers?: Record<string, string> | (() => Record<string, string>);
35
+ headers?: EventSourcePlusOptions["headers"];
36
36
  params?: TParams;
37
37
  parser: (input: unknown) => TType;
38
38
  serializer: (input: TParams) => TParams extends undefined ? undefined : string;
@@ -91,7 +91,7 @@ interface WsOptions<TResponse> {
91
91
  }
92
92
  interface ArriWsRequestOptions<TParams = any, TResponse = any> {
93
93
  url: string;
94
- headers?: Record<string, string> | (() => Record<string, string>);
94
+ headers?: EventSourcePlusOptions["headers"];
95
95
  params?: TParams;
96
96
  parser: (input: unknown) => TResponse;
97
97
  serializer: (input: TParams) => string;
@@ -102,7 +102,7 @@ interface ArriWsRequestOptions<TParams = any, TResponse = any> {
102
102
  onClose?: () => any;
103
103
  clientVersion?: string;
104
104
  }
105
- declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): WsController<TParams, TResponse>;
105
+ declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): Promise<WsController<TParams, TResponse>>;
106
106
  type WsErrorHook = (err: ArriErrorInstance) => void;
107
107
  type WsMessageHook<TResponse> = (msg: TResponse) => any;
108
108
  declare class WsController<TParams, TResponse> {
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { HttpMethod, OnRequestContext, OnRequestErrorContext, OnResponseContext, OnResponseErrorContext, EventSourceController } from 'event-source-plus';
1
+ import { HttpMethod, EventSourcePlusOptions, OnRequestContext, OnRequestErrorContext, OnResponseContext, OnResponseErrorContext, EventSourceController } from 'event-source-plus';
2
2
  export * from 'event-source-plus';
3
3
 
4
4
  interface ArriError {
@@ -32,7 +32,7 @@ declare class ArriErrorInstance extends Error implements ArriError {
32
32
  interface ArriRequestOpts<TType, TParams extends Record<any, any> | undefined = undefined> {
33
33
  url: string;
34
34
  method: HttpMethod;
35
- headers?: Record<string, string> | (() => Record<string, string>);
35
+ headers?: EventSourcePlusOptions["headers"];
36
36
  params?: TParams;
37
37
  parser: (input: unknown) => TType;
38
38
  serializer: (input: TParams) => TParams extends undefined ? undefined : string;
@@ -91,7 +91,7 @@ interface WsOptions<TResponse> {
91
91
  }
92
92
  interface ArriWsRequestOptions<TParams = any, TResponse = any> {
93
93
  url: string;
94
- headers?: Record<string, string> | (() => Record<string, string>);
94
+ headers?: EventSourcePlusOptions["headers"];
95
95
  params?: TParams;
96
96
  parser: (input: unknown) => TResponse;
97
97
  serializer: (input: TParams) => string;
@@ -102,7 +102,7 @@ interface ArriWsRequestOptions<TParams = any, TResponse = any> {
102
102
  onClose?: () => any;
103
103
  clientVersion?: string;
104
104
  }
105
- declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): WsController<TParams, TResponse>;
105
+ declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): Promise<WsController<TParams, TResponse>>;
106
106
  type WsErrorHook = (err: ArriErrorInstance) => void;
107
107
  type WsMessageHook<TResponse> = (msg: TResponse) => any;
108
108
  declare class WsController<TParams, TResponse> {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { HttpMethod, OnRequestContext, OnRequestErrorContext, OnResponseContext, OnResponseErrorContext, EventSourceController } from 'event-source-plus';
1
+ import { HttpMethod, EventSourcePlusOptions, OnRequestContext, OnRequestErrorContext, OnResponseContext, OnResponseErrorContext, EventSourceController } from 'event-source-plus';
2
2
  export * from 'event-source-plus';
3
3
 
4
4
  interface ArriError {
@@ -32,7 +32,7 @@ declare class ArriErrorInstance extends Error implements ArriError {
32
32
  interface ArriRequestOpts<TType, TParams extends Record<any, any> | undefined = undefined> {
33
33
  url: string;
34
34
  method: HttpMethod;
35
- headers?: Record<string, string> | (() => Record<string, string>);
35
+ headers?: EventSourcePlusOptions["headers"];
36
36
  params?: TParams;
37
37
  parser: (input: unknown) => TType;
38
38
  serializer: (input: TParams) => TParams extends undefined ? undefined : string;
@@ -91,7 +91,7 @@ interface WsOptions<TResponse> {
91
91
  }
92
92
  interface ArriWsRequestOptions<TParams = any, TResponse = any> {
93
93
  url: string;
94
- headers?: Record<string, string> | (() => Record<string, string>);
94
+ headers?: EventSourcePlusOptions["headers"];
95
95
  params?: TParams;
96
96
  parser: (input: unknown) => TResponse;
97
97
  serializer: (input: TParams) => string;
@@ -102,7 +102,7 @@ interface ArriWsRequestOptions<TParams = any, TResponse = any> {
102
102
  onClose?: () => any;
103
103
  clientVersion?: string;
104
104
  }
105
- declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): WsController<TParams, TResponse>;
105
+ declare function arriWsRequest<TParams extends Record<any, any> | undefined = undefined, TResponse = any>(opts: ArriWsRequestOptions<TParams, TResponse>, retryCount?: number): Promise<WsController<TParams, TResponse>>;
106
106
  type WsErrorHook = (err: ArriErrorInstance) => void;
107
107
  type WsMessageHook<TResponse> = (msg: TResponse) => any;
108
108
  declare class WsController<TParams, TResponse> {
package/dist/index.mjs CHANGED
@@ -66,6 +66,17 @@ class ArriErrorInstance extends Error {
66
66
  }
67
67
  }
68
68
 
69
+ async function getHeaders(input) {
70
+ if (typeof input === "function") {
71
+ const result = input();
72
+ if ("then" in result && typeof result.then === "function") {
73
+ return result.then((data) => data);
74
+ }
75
+ return result;
76
+ }
77
+ return input;
78
+ }
79
+
69
80
  async function arriRequest(opts) {
70
81
  let url = opts.url;
71
82
  let body;
@@ -89,12 +100,7 @@ async function arriRequest(opts) {
89
100
  break;
90
101
  }
91
102
  try {
92
- let headers = {};
93
- if (typeof opts.headers === "function") {
94
- headers = opts.headers();
95
- } else {
96
- headers = opts.headers ?? {};
97
- }
103
+ const headers = await getHeaders(opts.headers) ?? {};
98
104
  if (contentType)
99
105
  headers["Content-Type"] = contentType;
100
106
  if (opts.clientVersion)
@@ -177,13 +183,8 @@ function arriSseRequest(opts, options) {
177
183
  }
178
184
  const eventSource = new EventSourcePlus(url, {
179
185
  method: opts.method ?? "get",
180
- headers: () => {
181
- let headers;
182
- if (typeof opts.headers === "function") {
183
- headers = opts.headers();
184
- } else {
185
- headers = opts.headers ?? {};
186
- }
186
+ headers: async () => {
187
+ const headers = await getHeaders(opts.headers) ?? {};
187
188
  if (opts.clientVersion) {
188
189
  headers["client-version"] = opts.clientVersion;
189
190
  }
@@ -304,14 +305,9 @@ function connectWebsocket(url, protocol) {
304
305
  protocol
305
306
  });
306
307
  }
307
- function arriWsRequest(opts, retryCount = 0) {
308
+ async function arriWsRequest(opts, retryCount = 0) {
308
309
  let url = opts.url.replace("http://", "ws://").replace("https://", "wss://");
309
- let headers;
310
- if (typeof opts.headers === "function") {
311
- headers = opts.headers();
312
- } else {
313
- headers = opts.headers;
314
- }
310
+ const headers = await getHeaders(opts.headers);
315
311
  if (headers) {
316
312
  if (opts.clientVersion)
317
313
  headers["client-version"] = opts.clientVersion;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/client",
3
- "version": "0.46.0",
3
+ "version": "0.48.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -22,10 +22,10 @@
22
22
  "directory": "languages/ts/ts-client"
23
23
  },
24
24
  "dependencies": {
25
- "event-source-plus": "^0.0.6",
25
+ "event-source-plus": "^0.1.0",
26
26
  "ofetch": "^1.3.4",
27
27
  "ws": "^8.17.0",
28
- "@arrirpc/schema": "0.46.0"
28
+ "@arrirpc/schema": "0.48.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/ws": "^8.5.10"