@centia-io/sdk 0.0.27 → 0.0.29

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MapCentia ApS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,30 +1,36 @@
1
- # @centia-io/sdk
1
+ # SDK
2
2
 
3
- TypeScript/JavaScript client SDK for Centia GC2. It provides:
3
+ TypeScript/JavaScript client SDK for Centia-io. It provides:
4
4
 
5
5
  - Authentication helpers:
6
- - CodeFlow (OAuth 2.0 Authorization Code + PKCE) for browser apps
7
- - PasswordFlow for trusted/CLI/server environments
6
+ - CodeFlow (OAuth 2.0 Authorization Code + PKCE) for browser apps
7
+ - PasswordFlow for trusted/CLI/server environments
8
8
  - Data access:
9
- - Sql: Execute parameterized SQL
10
- - Rpc: Call JSON-RPC methods
11
- - createApi: A tiny type-safe helper that maps TypeScript interfaces to JSON‑RPC calls
12
-
13
- This README focuses on CodeFlow, PasswordFlow, Sql, Rpc and Api.ts (createApi).
9
+ - Sql: Execute parameterized SQL
10
+ - Rpc: Call JSON-RPC methods
11
+ - createApi: A tiny type-safe helper that maps TypeScript interfaces to JSON‑RPC calls
14
12
 
15
13
  ## Installation
16
14
 
17
- - npm: `npm install @centia-io/sdk`
18
- - pnpm: `pnpm add @centia-io/sdk`
19
- - yarn: `yarn add @centia-io/sdk`
15
+ ```bash
16
+ npm install @centia-io/sdk
17
+ yarn add @centia-io/sdk
18
+ pnpm add @centia-io/sdk
19
+ ```
20
+
21
+ Or from CDN:
22
+
23
+ ```html
24
+ <script src="https://cdn.jsdelivr.net/npm/@centia-io/sdk@latest/dist/centia-io-sdk.umd.js"></script>
25
+ ```
20
26
 
21
27
  Requirements:
22
28
  - Browser or Node.js 18+ (for global `fetch`).
23
- - An accessible GC2 host URL and client credentials.
29
+ - An accessible Centia.io host URL and client credentials.
24
30
 
25
31
  ESM import:
26
32
  ```ts
27
- import { CodeFlow, PasswordFlow, Sql, Rpc, createApi } from "@centia-io/sdk";
33
+ import { CodeFlow, PasswordFlow, Sql, Rpc, createApi, SignUp } from "@centia-io/sdk";
28
34
  import type { RpcRequest, RpcResponse, PgTypes } from "@centia-io/sdk";
29
35
  ```
30
36
 
@@ -38,23 +44,22 @@ The SDK handles token storage and refresh for you.
38
44
 
39
45
  ### CodeFlow (Browser, OAuth 2.0 Authorization Code + PKCE)
40
46
 
41
- Use this flow in browser applications where you can redirect the user to the GC2 login page.
47
+ Use this flow in browser applications where you can redirect the user to the Centia-io login page.
42
48
 
43
49
  Required options:
44
- - `host`: Base URL of your GC2 instance, e.g. `https://your.gc2.host`
45
- - `clientId`: OAuth client id configured in GC2
46
- - `redirectUri`: The URL in your app that handles the redirect back from GC2 (must be whitelisted)
47
- - `scope` (optional): space‑separated scopes, e.g. `"openid profile"`
50
+ - `host`: Base URL of your Centia-io instance, e.g. `https://api.centia.io`
51
+ - `clientId`: OAuth client id configured in Centia.io
52
+ - `redirectUri`: The URL in your app that handles the redirect back from Centia.io (must be whitelisted)
53
+ - `scope` (optional): Not in use yet, but will be used to request additional permissions from the user.
48
54
 
49
55
  Example (vanilla JS/TS + SPA):
50
56
  ```ts
51
57
  import { CodeFlow } from "@centia-io/sdk";
52
58
 
53
59
  const codeFlow = new CodeFlow({
54
- host: "https://your.gc2.host",
60
+ host: "https://api.centia.io",
55
61
  clientId: "your-client-id",
56
- redirectUri: window.location.origin + "/auth/callback",
57
- scope: "openid"
62
+ redirectUri: window.location.origin + "/auth/callback"
58
63
  });
59
64
 
60
65
  // On app startup, call redirectHandle() once to complete a login redirect (if any)
@@ -76,7 +81,7 @@ function onLogoutClick() {
76
81
  ```
77
82
 
78
83
  Notes:
79
- - `redirectHandle()` detects errors from the auth server, validates `state` (CSRF protection), exchanges the `code` for tokens, stores tokens and cleans up the URL.
84
+ - `redirectHandle()` detects errors from the auth server, validates `state` (CSRF protection), exchanges the `code` for tokens, performs `PKCE` (Proof Key for Code Exchange), stores tokens and cleans up the URL.
80
85
  - `signOut()` clears local tokens/options and redirects to the sign-out URL. If you only need to clear local state without redirect, call `codeFlow.clear()`.
81
86
 
82
87
  ### PasswordFlow (Trusted environments, CLI/Server)
@@ -95,11 +100,11 @@ Example (Node.js):
95
100
  import { PasswordFlow } from "@centia-io/sdk";
96
101
 
97
102
  const flow = new PasswordFlow({
98
- host: "http://localhost:8080",
99
- clientId: "gc2-cli",
100
- username: "mydb",
101
- password: "secret",
102
- database: "mydb"
103
+ host: "https://api.centia.io",
104
+ clientId: "your-client-id",
105
+ username: "your-username",
106
+ password: "your-password",
107
+ database: "parent-database" // The database to connect to. If superuser, this is the sam as username.
103
108
  });
104
109
 
105
110
  await flow.signIn();
@@ -110,7 +115,37 @@ await flow.signIn();
110
115
  flow.signOut(); // Clears tokens/options in local storage (no redirect)
111
116
  ```
112
117
 
113
- ---
118
+ ### SignUp (Browser – Create a new user)
119
+
120
+ Use this helper in browser applications to redirect the user to the Centia‑io sign‑up page.
121
+ The user will create an account under a specified parent database/tenant and then be redirected back to your application.
122
+
123
+ Required options:
124
+ - host: Base URL of your Centia‑io instance, e.g. https://api.centia.io
125
+ - clientId: OAuth client id configured in Centia.io
126
+ - parentDb: The parent/tenant database under which the new user should be created
127
+ - redirectUri: URL in your app to return to after sign‑up
128
+
129
+ Example (vanilla JS/TS):
130
+ ```ts
131
+ import { SignUp } from "@centia-io/sdk";
132
+
133
+ const signUp = new SignUp({
134
+ host: "https://api.centia.io",
135
+ clientId: "your-client-id",
136
+ parentDb: "your-parent-database",
137
+ redirectUri: window.location.origin + "/auth/callback"
138
+ });
139
+
140
+ // Start sign-up when the user clicks "Create account"
141
+ function onSignUpClick() {
142
+ signUp.signUp(); // Redirects to GC2 sign-up page
143
+ }
144
+ ```
145
+
146
+ Notes:
147
+ - Default endpoint is {host}/signup/. You can override with authUri if needed.
148
+ - After the user completes sign‑up and is redirected back to your app, start your normal sign‑in flow (e.g., CodeFlow. A session is started when the user signed up, so the user will be signed in automatically in the flow.)
114
149
 
115
150
  ## SQL
116
151
 
@@ -118,17 +153,17 @@ Execute parameterized SQL against GC2.
118
153
 
119
154
  - Class: `new Sql()`
120
155
  - Method: `exec(request: SqlRequest): Promise<SQLResponse>`
121
- - Endpoint: `POST {host}/api/v4/sql`
156
+ - Endpoint: `POST https://api.centia.io/api/v4/sql`
122
157
 
123
158
  Types (simplified):
124
159
  - `SqlRequest` has:
125
- - `q`: SQL string, you can use named placeholders like `:a` (server-side feature)
126
- - `params?`: object with values for placeholders
127
- - `type_hints?`: optional explicit type hints
128
- - `type_formats?`: optional per-column format strings
160
+ - `q`: SQL string, you can use named placeholders like `:a` (server-side feature)
161
+ - `params?`: object with values for placeholders
162
+ - `type_hints?`: optional explicit type hints
163
+ - `type_formats?`: optional per-column format strings
129
164
  - `SQLResponse` has:
130
- - `schema`: a map of column name -> `{ type: string, array: boolean }`
131
- - `data`: an array of rows (records)
165
+ - `schema`: a map of column name -> `{ type: string, array: boolean }`
166
+ - `data`: an array of rows (records)
132
167
 
133
168
  Example:
134
169
  ```ts
@@ -146,7 +181,8 @@ const payload = {
146
181
 
147
182
  const res = await sql.exec({
148
183
  q: "select :a::int as a, :b::varchar as b, :c::numeric as c, :d::varchar[] as d, :e::jsonb as e",
149
- params: payload
184
+ params: payload,
185
+ type_hints: { d: "varchar[]" } // Arrays are not inferred by default, and must be specified explicitly
150
186
  });
151
187
 
152
188
  console.log(res.schema); // { a: {type: 'int4', array: false}, ... }
@@ -159,9 +195,9 @@ import type { PgTypes } from "@centia-io/sdk";
159
195
 
160
196
  interface Row extends PgTypes.DataRow {
161
197
  a: number;
162
- b: string;
198
+ b: Pgtypes.Varchar;
163
199
  c: PgTypes.NumericString;
164
- d: PgTypes.PgArray<string>;
200
+ d: PgTypes.PgArray<Pgtypes.Varchar>;
165
201
  e: PgTypes.JsonValue;
166
202
  }
167
203
 
@@ -169,8 +205,6 @@ interface Row extends PgTypes.DataRow {
169
205
  const res = await sql.exec({ q: "...", params: payload }) as PgTypes.SQLResponse<Row>;
170
206
  ```
171
207
 
172
- ---
173
-
174
208
  ## RPC
175
209
 
176
210
  Call JSON‑RPC methods exposed by GC2.
@@ -214,9 +248,7 @@ interface Row extends PgTypes.DataRow {
214
248
  const res = await rpc.call({ jsonrpc: "2.0", method: "typeTest", params: payload }) as PgTypes.RpcResponse<Row>;
215
249
  ```
216
250
 
217
- ---
218
-
219
- ## createApi (Api.ts)
251
+ ## createApi
220
252
 
221
253
  A tiny helper that builds a Proxy around `Rpc` so you can call `api.someMethod(params)` directly, with TypeScript autocompletion and type‑checking based on your own interface.
222
254
 
@@ -231,15 +263,15 @@ import type { PgTypes } from "@centia-io/sdk";
231
263
  interface MyApi {
232
264
  typeTest(params: {
233
265
  a: number;
234
- b: string;
266
+ b: Pgtypes.Varchar;
235
267
  c: PgTypes.NumericString;
236
- d: PgTypes.PgArray<string>;
268
+ d: PgTypes.PgArray<Pgtypes.Varchar>;
237
269
  e: PgTypes.JsonValue;
238
270
  }): Promise<Array<{
239
271
  a: number;
240
- b: string;
272
+ b: Pgtypes.Varchar;
241
273
  c: PgTypes.NumericString;
242
- d: PgTypes.PgArray<string>;
274
+ d: PgTypes.PgArray<Pgtypes.Varchar>;
243
275
  e: PgTypes.JsonValue;
244
276
  }>>;
245
277
  }
@@ -248,10 +280,10 @@ const api = createApi<MyApi>();
248
280
 
249
281
  const rows = await api.typeTest({
250
282
  a: 1,
251
- b: "hej",
283
+ b: "Hello world",
252
284
  c: "3.4",
253
- d: ["ds", "sdsd"],
254
- e: { zdsd: [2,3,4,5,6,7,8,9,10] }
285
+ d: ["Hello", "world"],
286
+ e: { "x": [1,2,3,4,5,6,7,8,9,10] }
255
287
  });
256
288
 
257
289
  console.log(rows); // typed row array
@@ -261,28 +293,17 @@ Notes:
261
293
  - `createApi<T>()` relies on naming conventions: the property name is the JSON‑RPC `method` name.
262
294
  - Each call returns `result.data` from the RPC response (array of rows).
263
295
 
264
- ---
265
-
266
296
  ## Error handling
267
297
 
268
298
  - Network/HTTP errors: thrown as `Error` with the status/body text when available.
269
299
  - Auth errors: the SDK auto‑refreshes access tokens when possible. If the refresh token is expired or missing, you’ll get an error and should re‑authenticate.
270
300
 
271
- ---
272
301
 
273
302
  ## Environment details
274
303
 
275
304
  - Storage: tokens/options stored in `localStorage` when available; otherwise a global in‑memory store is used (`globalThis.__gc2_memory_storage`).
276
305
  - Fetch: Node.js 18+ recommended (includes native `fetch`). For older Node versions, add a Fetch polyfill.
277
306
 
278
- ---
279
-
280
- ## Examples
281
-
282
- See the `examples/` folder for runnable snippets showing PasswordFlow + Sql/Rpc and browser CodeFlow usage.
283
-
284
- ---
285
-
286
307
  ## License
287
308
 
288
- AGPL-3.0
309
+ The SDK is licensed under [The MIT License](https://opensource.org/license/mit)