@everystack/api 0.2.3 → 0.2.4
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/package.json +1 -1
- package/src/client/index.ts +23 -0
package/package.json
CHANGED
package/src/client/index.ts
CHANGED
|
@@ -76,6 +76,8 @@ export interface QueryBuilder {
|
|
|
76
76
|
|
|
77
77
|
export interface Client {
|
|
78
78
|
from(table: string): QueryBuilder;
|
|
79
|
+
/** Call a named RPC on the handler: POST {baseUrl}/rpc/{fn}. */
|
|
80
|
+
rpc<T = unknown>(fn: string, body?: unknown): Promise<ClientResponse<T>>;
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
/**
|
|
@@ -89,6 +91,27 @@ export function createClient(options: ClientOptions): Client {
|
|
|
89
91
|
from(table: string): QueryBuilder {
|
|
90
92
|
return createQueryBuilder(options, table);
|
|
91
93
|
},
|
|
94
|
+
|
|
95
|
+
async rpc<T = unknown>(fn: string, body?: unknown): Promise<ClientResponse<T>> {
|
|
96
|
+
const url = `${options.baseUrl}/rpc/${encodeURIComponent(fn)}`;
|
|
97
|
+
const headers = await buildHeaders(options);
|
|
98
|
+
headers['Content-Type'] = 'application/json';
|
|
99
|
+
|
|
100
|
+
const { response, retried } = await fetchWithRetry(
|
|
101
|
+
url,
|
|
102
|
+
{ method: 'POST', headers, body: JSON.stringify(body ?? {}) },
|
|
103
|
+
options,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
if (retried) return retried as ClientResponse<T>;
|
|
107
|
+
|
|
108
|
+
if (!response.ok) {
|
|
109
|
+
return { data: null, error: await parseError(response) };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const data = (await response.json()) as T;
|
|
113
|
+
return { data, error: null };
|
|
114
|
+
},
|
|
92
115
|
};
|
|
93
116
|
}
|
|
94
117
|
|