@squadbase/react 0.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/README.md +99 -0
- package/dist/index.cjs +77 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +54 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# @squadbase/react
|
|
2
|
+
|
|
3
|
+
React SDK for Squadbase - A package for handling client-side operations in Squadbase applications with React.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @squadbase/react
|
|
9
|
+
# or
|
|
10
|
+
yarn add @squadbase/react
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @squadbase/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
### User Session
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { SquadbaseProvider, useUser } from "@squadbase/react";
|
|
21
|
+
|
|
22
|
+
// Wrap your app with SquadbaseProvider
|
|
23
|
+
function App() {
|
|
24
|
+
return (
|
|
25
|
+
<SquadbaseProvider projectId="your-project-id">
|
|
26
|
+
<UserProfile />
|
|
27
|
+
</SquadbaseProvider>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Use the useUser hook in your components
|
|
32
|
+
function UserProfile() {
|
|
33
|
+
const user = useUser();
|
|
34
|
+
|
|
35
|
+
if (user.status === "pending") {
|
|
36
|
+
return <div>Loading...</div>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (user.status === "error") {
|
|
40
|
+
return <div>Error: {user.error}</div>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<div>
|
|
45
|
+
<h1>Welcome, {user.data.firstName}!</h1>
|
|
46
|
+
<p>Email: {user.data.email}</p>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Local Development
|
|
53
|
+
|
|
54
|
+
For local development, you can provide a mock user:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
<SquadbaseProvider
|
|
58
|
+
projectId="your-project-id"
|
|
59
|
+
mockUser={{
|
|
60
|
+
username: "test-user",
|
|
61
|
+
email: "test@example.com",
|
|
62
|
+
firstName: "Test",
|
|
63
|
+
lastName: "User",
|
|
64
|
+
iconUrl: null,
|
|
65
|
+
roles: ["user"],
|
|
66
|
+
}}
|
|
67
|
+
>
|
|
68
|
+
<App />
|
|
69
|
+
</SquadbaseProvider>
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## API Reference
|
|
73
|
+
|
|
74
|
+
### `SquadbaseProvider`
|
|
75
|
+
|
|
76
|
+
A React context provider that wraps your application and provides access to the Squadbase client.
|
|
77
|
+
|
|
78
|
+
#### Props
|
|
79
|
+
|
|
80
|
+
- `projectId` (string): Your Squadbase project ID
|
|
81
|
+
- `mockUser` (optional): Mock user object for local development
|
|
82
|
+
|
|
83
|
+
### `useUser()`
|
|
84
|
+
|
|
85
|
+
A React hook that returns the current authenticated user.
|
|
86
|
+
|
|
87
|
+
#### Returns
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
type QueryResult<User, string> = {
|
|
91
|
+
status: "pending" | "success" | "error";
|
|
92
|
+
data?: User;
|
|
93
|
+
error?: string;
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
SquadbaseProvider: () => SquadbaseProvider,
|
|
24
|
+
useUser: () => useUser
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/user.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
var import_zod = require("zod");
|
|
31
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
32
|
+
var zUser = import_zod.z.object({
|
|
33
|
+
username: import_zod.z.string(),
|
|
34
|
+
email: import_zod.z.string(),
|
|
35
|
+
firstName: import_zod.z.string(),
|
|
36
|
+
lastName: import_zod.z.string(),
|
|
37
|
+
iconUrl: import_zod.z.string().nullable(),
|
|
38
|
+
roles: import_zod.z.array(import_zod.z.string())
|
|
39
|
+
});
|
|
40
|
+
async function fetchUser() {
|
|
41
|
+
const response = await fetch("/_sqcore/auth", { method: "POST" });
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
throw new Error("Failed to get user");
|
|
44
|
+
}
|
|
45
|
+
return zUser.parse(await response.json());
|
|
46
|
+
}
|
|
47
|
+
var UserContext = (0, import_react.createContext)(null);
|
|
48
|
+
function UserProvider({ children }) {
|
|
49
|
+
const [state, setState] = (0, import_react.useState)({ status: "pending" });
|
|
50
|
+
(0, import_react.useEffect)(() => {
|
|
51
|
+
fetchUser().then((data) => setState({ status: "success", data })).catch(
|
|
52
|
+
(e) => setState({
|
|
53
|
+
status: "error",
|
|
54
|
+
error: e instanceof Error ? e.message : "Failed to get user"
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
}, []);
|
|
58
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(UserContext.Provider, { value: state, children });
|
|
59
|
+
}
|
|
60
|
+
function useUser() {
|
|
61
|
+
const state = (0, import_react.useContext)(UserContext);
|
|
62
|
+
if (!state) {
|
|
63
|
+
throw new Error("<SquadbaseProvider> is not found");
|
|
64
|
+
}
|
|
65
|
+
return state;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/provider.tsx
|
|
69
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
70
|
+
function SquadbaseProvider({ children }) {
|
|
71
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(UserProvider, { children });
|
|
72
|
+
}
|
|
73
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
+
0 && (module.exports = {
|
|
75
|
+
SquadbaseProvider,
|
|
76
|
+
useUser
|
|
77
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
declare function SquadbaseProvider({ children }: {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}): react_jsx_runtime.JSX.Element;
|
|
8
|
+
|
|
9
|
+
declare const zUser: z.ZodObject<{
|
|
10
|
+
username: z.ZodString;
|
|
11
|
+
email: z.ZodString;
|
|
12
|
+
firstName: z.ZodString;
|
|
13
|
+
lastName: z.ZodString;
|
|
14
|
+
iconUrl: z.ZodNullable<z.ZodString>;
|
|
15
|
+
roles: z.ZodArray<z.ZodString>;
|
|
16
|
+
}, z.core.$strip>;
|
|
17
|
+
type User = z.infer<typeof zUser>;
|
|
18
|
+
type UserState = {
|
|
19
|
+
status: "pending";
|
|
20
|
+
} | {
|
|
21
|
+
status: "success";
|
|
22
|
+
data: User;
|
|
23
|
+
} | {
|
|
24
|
+
status: "error";
|
|
25
|
+
error: string;
|
|
26
|
+
};
|
|
27
|
+
declare function useUser(): UserState;
|
|
28
|
+
|
|
29
|
+
export { SquadbaseProvider, type User, type UserState, useUser };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
declare function SquadbaseProvider({ children }: {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}): react_jsx_runtime.JSX.Element;
|
|
8
|
+
|
|
9
|
+
declare const zUser: z.ZodObject<{
|
|
10
|
+
username: z.ZodString;
|
|
11
|
+
email: z.ZodString;
|
|
12
|
+
firstName: z.ZodString;
|
|
13
|
+
lastName: z.ZodString;
|
|
14
|
+
iconUrl: z.ZodNullable<z.ZodString>;
|
|
15
|
+
roles: z.ZodArray<z.ZodString>;
|
|
16
|
+
}, z.core.$strip>;
|
|
17
|
+
type User = z.infer<typeof zUser>;
|
|
18
|
+
type UserState = {
|
|
19
|
+
status: "pending";
|
|
20
|
+
} | {
|
|
21
|
+
status: "success";
|
|
22
|
+
data: User;
|
|
23
|
+
} | {
|
|
24
|
+
status: "error";
|
|
25
|
+
error: string;
|
|
26
|
+
};
|
|
27
|
+
declare function useUser(): UserState;
|
|
28
|
+
|
|
29
|
+
export { SquadbaseProvider, type User, type UserState, useUser };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/user.tsx
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useState
|
|
7
|
+
} from "react";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { jsx } from "react/jsx-runtime";
|
|
10
|
+
var zUser = z.object({
|
|
11
|
+
username: z.string(),
|
|
12
|
+
email: z.string(),
|
|
13
|
+
firstName: z.string(),
|
|
14
|
+
lastName: z.string(),
|
|
15
|
+
iconUrl: z.string().nullable(),
|
|
16
|
+
roles: z.array(z.string())
|
|
17
|
+
});
|
|
18
|
+
async function fetchUser() {
|
|
19
|
+
const response = await fetch("/_sqcore/auth", { method: "POST" });
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
throw new Error("Failed to get user");
|
|
22
|
+
}
|
|
23
|
+
return zUser.parse(await response.json());
|
|
24
|
+
}
|
|
25
|
+
var UserContext = createContext(null);
|
|
26
|
+
function UserProvider({ children }) {
|
|
27
|
+
const [state, setState] = useState({ status: "pending" });
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
fetchUser().then((data) => setState({ status: "success", data })).catch(
|
|
30
|
+
(e) => setState({
|
|
31
|
+
status: "error",
|
|
32
|
+
error: e instanceof Error ? e.message : "Failed to get user"
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
}, []);
|
|
36
|
+
return /* @__PURE__ */ jsx(UserContext.Provider, { value: state, children });
|
|
37
|
+
}
|
|
38
|
+
function useUser() {
|
|
39
|
+
const state = useContext(UserContext);
|
|
40
|
+
if (!state) {
|
|
41
|
+
throw new Error("<SquadbaseProvider> is not found");
|
|
42
|
+
}
|
|
43
|
+
return state;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/provider.tsx
|
|
47
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
48
|
+
function SquadbaseProvider({ children }) {
|
|
49
|
+
return /* @__PURE__ */ jsx2(UserProvider, { children });
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
SquadbaseProvider,
|
|
53
|
+
useUser
|
|
54
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@squadbase/react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/index.d.cts",
|
|
18
|
+
"default": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup ./src/index.ts",
|
|
24
|
+
"dev": "tsup ./src/index.ts --watch"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "Squadbase",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"packageManager": "pnpm@10.6.3",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"zod": "^4.3.6"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/react": "^19.1.2",
|
|
38
|
+
"react": "^19.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@types/react": "17.x || 18.x || 19.x",
|
|
42
|
+
"react": "17.x || 18.x || 19.x"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"@types/react": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|