@anythink-cloud/sdk 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Anythink Ltd
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 ADDED
@@ -0,0 +1,52 @@
1
+ # @anythink-cloud/sdk
2
+
3
+ Reusable Typescript SDK for the Anythink platform
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @anythink-cloud/sdk
9
+ # or
10
+ yarn add @anythink-cloud/sdk
11
+ # or
12
+ pnpm add @anythink-cloud/sdk
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import /* your exports */ from "@anythink-cloud/sdk";
19
+ ```
20
+
21
+ ## Development
22
+
23
+ ```bash
24
+ # Install dependencies
25
+ npm install
26
+
27
+ # Build the package
28
+ npm run build
29
+
30
+ # Watch mode for development
31
+ npm run dev
32
+
33
+ # Type checking
34
+ npm run typecheck
35
+
36
+ # Linting
37
+ npm run lint
38
+ ```
39
+
40
+ ## Publishing
41
+
42
+ ```bash
43
+ # Build before publishing
44
+ npm run build
45
+
46
+ # Publish to npm
47
+ npm publish
48
+ ```
49
+
50
+ ## License
51
+
52
+ MIT
@@ -0,0 +1,229 @@
1
+ import * as zustand_middleware from 'zustand/middleware';
2
+ import * as zustand from 'zustand';
3
+ import { AxiosInstance, AxiosRequestConfig } from 'axios';
4
+
5
+ /**
6
+ * Token pair structure matching the backend API response
7
+ */
8
+ interface TokenPair {
9
+ access_token: string;
10
+ refresh_token: string;
11
+ expires_in: number;
12
+ }
13
+ /**
14
+ * Session structure for internal use
15
+ */
16
+ interface Session {
17
+ access_token: string;
18
+ refresh_token: string;
19
+ expires_in: number;
20
+ expires_at: number;
21
+ }
22
+ /**
23
+ * Configuration for the auth client
24
+ */
25
+ interface AuthConfig {
26
+ instanceUrl: string;
27
+ orgId: number;
28
+ tokenEndpoint?: string;
29
+ refreshEndpoint?: string;
30
+ changePasswordEndpoint?: string;
31
+ storageKey?: string;
32
+ onSignOut?: () => void;
33
+ }
34
+ /**
35
+ * Sign in response
36
+ */
37
+ interface SignInResponse {
38
+ data: {
39
+ session: Session | null;
40
+ };
41
+ error: Error | null;
42
+ }
43
+ /**
44
+ * Refresh response
45
+ */
46
+ interface RefreshResponse {
47
+ data: {
48
+ session: Session | null;
49
+ };
50
+ error: Error | null;
51
+ }
52
+
53
+ interface AuthState {
54
+ session: Session | null;
55
+ isLoading: boolean;
56
+ error: Error | null;
57
+ setSession: (session: Session | null) => void;
58
+ signOut: () => void;
59
+ clearError: () => void;
60
+ setLoading: (isLoading: boolean) => void;
61
+ setError: (error: Error | null) => void;
62
+ }
63
+ /**
64
+ * Creates the auth store with persistence
65
+ * @param storageKey - Key for localStorage persistence
66
+ */
67
+ declare const createAuthStore: (storageKey?: string) => zustand.UseBoundStore<Omit<zustand.StoreApi<AuthState>, "setState" | "persist"> & {
68
+ setState(partial: AuthState | Partial<AuthState> | ((state: AuthState) => AuthState | Partial<AuthState>), replace?: false | undefined): unknown;
69
+ setState(state: AuthState | ((state: AuthState) => AuthState), replace: true): unknown;
70
+ persist: {
71
+ setOptions: (options: Partial<zustand_middleware.PersistOptions<AuthState, unknown, unknown>>) => void;
72
+ clearStorage: () => void;
73
+ rehydrate: () => Promise<void> | void;
74
+ hasHydrated: () => boolean;
75
+ onHydrate: (fn: (state: AuthState) => void) => () => void;
76
+ onFinishHydration: (fn: (state: AuthState) => void) => () => void;
77
+ getOptions: () => Partial<zustand_middleware.PersistOptions<AuthState, unknown, unknown>>;
78
+ };
79
+ }>;
80
+
81
+ /**
82
+ * Auth client for handling authentication with the Anythink API
83
+ */
84
+ declare class AuthClient {
85
+ private store;
86
+ private config;
87
+ private onSessionChanged?;
88
+ private axiosClient;
89
+ constructor(config: AuthConfig & {
90
+ onSessionChanged?: (session: Session | null) => void;
91
+ });
92
+ /**
93
+ * Set the onSessionChanged handler.
94
+ * @param handler Callback fired whenever the session changes
95
+ */
96
+ setOnSessionChanged(handler: ((session: Session | null) => void) | undefined): void;
97
+ /**
98
+ * Internal helper to call the session-changed handler, if present.
99
+ */
100
+ private _callSessionChanged;
101
+ /**
102
+ * Sign in with email and password
103
+ * @param email User email
104
+ * @param password User password
105
+ * @param orgId Optional organization ID
106
+ * @returns Session object with tokens
107
+ */
108
+ signIn(email: string, password: string, orgId?: number): Promise<SignInResponse>;
109
+ /**
110
+ * Register a new user
111
+ * @param firstName User's first name
112
+ * @param lastName User's last name
113
+ * @param email User's email
114
+ * @param password User's password
115
+ * @returns Error object or null if successful
116
+ */
117
+ register(firstName: string, lastName: string, email: string, password: string): Promise<{
118
+ error: Error | null;
119
+ }>;
120
+ /**
121
+ * Refresh the access token using the refresh token
122
+ * @returns Session object with new tokens
123
+ */
124
+ refreshSession(): Promise<RefreshResponse>;
125
+ /**
126
+ * Get the current session
127
+ * @returns Session object or null if not authenticated
128
+ */
129
+ getSession(): {
130
+ data: {
131
+ session: Session | null;
132
+ };
133
+ };
134
+ /**
135
+ * Set session from tokens (useful for OAuth flows or token exchange)
136
+ * @param accessToken Access token
137
+ * @param refreshToken Refresh token
138
+ * @param expiresIn Expiration time in seconds
139
+ */
140
+ setSession({ access_token, refresh_token, expires_in, }: {
141
+ access_token: string;
142
+ refresh_token: string;
143
+ expires_in: number;
144
+ }): Promise<{
145
+ error: Error | null;
146
+ }>;
147
+ /**
148
+ * Change the current user's password
149
+ * @param currentPassword Current password
150
+ * @param newPassword New password
151
+ * @returns Error object or null if successful
152
+ */
153
+ changePassword(currentPassword: string, newPassword: string): Promise<{
154
+ error: Error | null;
155
+ }>;
156
+ /**
157
+ * Sign out and clear session
158
+ */
159
+ signOut(): Promise<{
160
+ error: null;
161
+ }>;
162
+ /**
163
+ * Get the current access token
164
+ */
165
+ getAccessToken(): string | null;
166
+ /**
167
+ * Get the current refresh token
168
+ */
169
+ getRefreshToken(): string | null;
170
+ /**
171
+ * Check if user is authenticated
172
+ */
173
+ isAuthenticated(): boolean;
174
+ /**
175
+ * Get the Zustand store (for React hooks)
176
+ */
177
+ getStore(): ReturnType<typeof createAuthStore>;
178
+ }
179
+
180
+ /**
181
+ * Base service class with automatic token injection and refresh handling
182
+ */
183
+ declare class AuthenticatedBaseService {
184
+ client: AxiosInstance;
185
+ private authClient;
186
+ private instanceUrl;
187
+ constructor(authClient: AuthClient, instanceUrl: string);
188
+ /**
189
+ * Setup request and response interceptors
190
+ */
191
+ private setupInterceptors;
192
+ /**
193
+ * GET request
194
+ */
195
+ protected get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
196
+ /**
197
+ * POST request
198
+ */
199
+ protected post<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosRequestConfig): Promise<TResponse>;
200
+ /**
201
+ * POST request with form data
202
+ */
203
+ protected postFormData<FormData, TResponse>(url: string, data: FormData): Promise<TResponse>;
204
+ /**
205
+ * PUT request
206
+ */
207
+ protected put<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosRequestConfig): Promise<TResponse>;
208
+ /**
209
+ * PATCH request
210
+ */
211
+ protected patch<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosRequestConfig): Promise<TResponse>;
212
+ /**
213
+ * DELETE request
214
+ */
215
+ protected delete<T = void>(url: string, config?: AxiosRequestConfig): Promise<T>;
216
+ /**
217
+ * Get the underlying Axios instance (for advanced usage)
218
+ */
219
+ getClient(): AxiosInstance;
220
+ }
221
+
222
+ /**
223
+ * Anythink SDK
224
+ *
225
+ * A reusable Typescript SDK for the Anythink platform.
226
+ */
227
+ declare const version = "0.1.0";
228
+
229
+ export { AuthClient, type AuthConfig, AuthenticatedBaseService, type RefreshResponse, type Session, type SignInResponse, type TokenPair, createAuthStore, version };
@@ -0,0 +1,229 @@
1
+ import * as zustand_middleware from 'zustand/middleware';
2
+ import * as zustand from 'zustand';
3
+ import { AxiosInstance, AxiosRequestConfig } from 'axios';
4
+
5
+ /**
6
+ * Token pair structure matching the backend API response
7
+ */
8
+ interface TokenPair {
9
+ access_token: string;
10
+ refresh_token: string;
11
+ expires_in: number;
12
+ }
13
+ /**
14
+ * Session structure for internal use
15
+ */
16
+ interface Session {
17
+ access_token: string;
18
+ refresh_token: string;
19
+ expires_in: number;
20
+ expires_at: number;
21
+ }
22
+ /**
23
+ * Configuration for the auth client
24
+ */
25
+ interface AuthConfig {
26
+ instanceUrl: string;
27
+ orgId: number;
28
+ tokenEndpoint?: string;
29
+ refreshEndpoint?: string;
30
+ changePasswordEndpoint?: string;
31
+ storageKey?: string;
32
+ onSignOut?: () => void;
33
+ }
34
+ /**
35
+ * Sign in response
36
+ */
37
+ interface SignInResponse {
38
+ data: {
39
+ session: Session | null;
40
+ };
41
+ error: Error | null;
42
+ }
43
+ /**
44
+ * Refresh response
45
+ */
46
+ interface RefreshResponse {
47
+ data: {
48
+ session: Session | null;
49
+ };
50
+ error: Error | null;
51
+ }
52
+
53
+ interface AuthState {
54
+ session: Session | null;
55
+ isLoading: boolean;
56
+ error: Error | null;
57
+ setSession: (session: Session | null) => void;
58
+ signOut: () => void;
59
+ clearError: () => void;
60
+ setLoading: (isLoading: boolean) => void;
61
+ setError: (error: Error | null) => void;
62
+ }
63
+ /**
64
+ * Creates the auth store with persistence
65
+ * @param storageKey - Key for localStorage persistence
66
+ */
67
+ declare const createAuthStore: (storageKey?: string) => zustand.UseBoundStore<Omit<zustand.StoreApi<AuthState>, "setState" | "persist"> & {
68
+ setState(partial: AuthState | Partial<AuthState> | ((state: AuthState) => AuthState | Partial<AuthState>), replace?: false | undefined): unknown;
69
+ setState(state: AuthState | ((state: AuthState) => AuthState), replace: true): unknown;
70
+ persist: {
71
+ setOptions: (options: Partial<zustand_middleware.PersistOptions<AuthState, unknown, unknown>>) => void;
72
+ clearStorage: () => void;
73
+ rehydrate: () => Promise<void> | void;
74
+ hasHydrated: () => boolean;
75
+ onHydrate: (fn: (state: AuthState) => void) => () => void;
76
+ onFinishHydration: (fn: (state: AuthState) => void) => () => void;
77
+ getOptions: () => Partial<zustand_middleware.PersistOptions<AuthState, unknown, unknown>>;
78
+ };
79
+ }>;
80
+
81
+ /**
82
+ * Auth client for handling authentication with the Anythink API
83
+ */
84
+ declare class AuthClient {
85
+ private store;
86
+ private config;
87
+ private onSessionChanged?;
88
+ private axiosClient;
89
+ constructor(config: AuthConfig & {
90
+ onSessionChanged?: (session: Session | null) => void;
91
+ });
92
+ /**
93
+ * Set the onSessionChanged handler.
94
+ * @param handler Callback fired whenever the session changes
95
+ */
96
+ setOnSessionChanged(handler: ((session: Session | null) => void) | undefined): void;
97
+ /**
98
+ * Internal helper to call the session-changed handler, if present.
99
+ */
100
+ private _callSessionChanged;
101
+ /**
102
+ * Sign in with email and password
103
+ * @param email User email
104
+ * @param password User password
105
+ * @param orgId Optional organization ID
106
+ * @returns Session object with tokens
107
+ */
108
+ signIn(email: string, password: string, orgId?: number): Promise<SignInResponse>;
109
+ /**
110
+ * Register a new user
111
+ * @param firstName User's first name
112
+ * @param lastName User's last name
113
+ * @param email User's email
114
+ * @param password User's password
115
+ * @returns Error object or null if successful
116
+ */
117
+ register(firstName: string, lastName: string, email: string, password: string): Promise<{
118
+ error: Error | null;
119
+ }>;
120
+ /**
121
+ * Refresh the access token using the refresh token
122
+ * @returns Session object with new tokens
123
+ */
124
+ refreshSession(): Promise<RefreshResponse>;
125
+ /**
126
+ * Get the current session
127
+ * @returns Session object or null if not authenticated
128
+ */
129
+ getSession(): {
130
+ data: {
131
+ session: Session | null;
132
+ };
133
+ };
134
+ /**
135
+ * Set session from tokens (useful for OAuth flows or token exchange)
136
+ * @param accessToken Access token
137
+ * @param refreshToken Refresh token
138
+ * @param expiresIn Expiration time in seconds
139
+ */
140
+ setSession({ access_token, refresh_token, expires_in, }: {
141
+ access_token: string;
142
+ refresh_token: string;
143
+ expires_in: number;
144
+ }): Promise<{
145
+ error: Error | null;
146
+ }>;
147
+ /**
148
+ * Change the current user's password
149
+ * @param currentPassword Current password
150
+ * @param newPassword New password
151
+ * @returns Error object or null if successful
152
+ */
153
+ changePassword(currentPassword: string, newPassword: string): Promise<{
154
+ error: Error | null;
155
+ }>;
156
+ /**
157
+ * Sign out and clear session
158
+ */
159
+ signOut(): Promise<{
160
+ error: null;
161
+ }>;
162
+ /**
163
+ * Get the current access token
164
+ */
165
+ getAccessToken(): string | null;
166
+ /**
167
+ * Get the current refresh token
168
+ */
169
+ getRefreshToken(): string | null;
170
+ /**
171
+ * Check if user is authenticated
172
+ */
173
+ isAuthenticated(): boolean;
174
+ /**
175
+ * Get the Zustand store (for React hooks)
176
+ */
177
+ getStore(): ReturnType<typeof createAuthStore>;
178
+ }
179
+
180
+ /**
181
+ * Base service class with automatic token injection and refresh handling
182
+ */
183
+ declare class AuthenticatedBaseService {
184
+ client: AxiosInstance;
185
+ private authClient;
186
+ private instanceUrl;
187
+ constructor(authClient: AuthClient, instanceUrl: string);
188
+ /**
189
+ * Setup request and response interceptors
190
+ */
191
+ private setupInterceptors;
192
+ /**
193
+ * GET request
194
+ */
195
+ protected get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
196
+ /**
197
+ * POST request
198
+ */
199
+ protected post<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosRequestConfig): Promise<TResponse>;
200
+ /**
201
+ * POST request with form data
202
+ */
203
+ protected postFormData<FormData, TResponse>(url: string, data: FormData): Promise<TResponse>;
204
+ /**
205
+ * PUT request
206
+ */
207
+ protected put<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosRequestConfig): Promise<TResponse>;
208
+ /**
209
+ * PATCH request
210
+ */
211
+ protected patch<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosRequestConfig): Promise<TResponse>;
212
+ /**
213
+ * DELETE request
214
+ */
215
+ protected delete<T = void>(url: string, config?: AxiosRequestConfig): Promise<T>;
216
+ /**
217
+ * Get the underlying Axios instance (for advanced usage)
218
+ */
219
+ getClient(): AxiosInstance;
220
+ }
221
+
222
+ /**
223
+ * Anythink SDK
224
+ *
225
+ * A reusable Typescript SDK for the Anythink platform.
226
+ */
227
+ declare const version = "0.1.0";
228
+
229
+ export { AuthClient, type AuthConfig, AuthenticatedBaseService, type RefreshResponse, type Session, type SignInResponse, type TokenPair, createAuthStore, version };