@ph-cms/client-sdk 0.1.2 → 0.1.3

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 CHANGED
@@ -126,6 +126,59 @@ export function App() {
126
126
  }
127
127
  ```
128
128
 
129
+ ## Media & File Upload (v0.1.2+)
130
+
131
+ Uploading media files follows a 3-step process: Request a Ticket -> Upload to S3 -> Create/Update Content.
132
+
133
+ ### 1. Simple Workflow Example
134
+
135
+ ```tsx
136
+ import { useMediaUploadTickets, useUploadToS3, useCreateContent } from '@ph-cms/client-sdk';
137
+
138
+ function MediaUploader() {
139
+ const { mutateAsync: getTickets } = useMediaUploadTickets();
140
+ const { mutateAsync: uploadToS3 } = useUploadToS3();
141
+ const { mutateAsync: createContent } = useCreateContent();
142
+
143
+ const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
144
+ const file = event.target.files?.[0];
145
+ if (!file) return;
146
+
147
+ // Step 1: Get Upload Ticket (Presigned URL)
148
+ const tickets = await getTickets([{
149
+ filename: file.name,
150
+ contentType: file.type,
151
+ fileSize: file.size,
152
+ // width: 1024, (optional)
153
+ // height: 768 (optional)
154
+ }]);
155
+
156
+ const { mediaUid, uploadUrl } = tickets[0];
157
+
158
+ // Step 2: Upload File directly to S3
159
+ await uploadToS3({ url: uploadUrl, file });
160
+
161
+ // Step 3: Use the mediaUid to create content
162
+ await createContent({
163
+ channelSlug: 'my-channel',
164
+ type: 'post',
165
+ title: 'Post with image',
166
+ mediaAttachments: [mediaUid]
167
+ });
168
+ };
169
+
170
+ return <input type="file" onChange={handleFileChange} />;
171
+ }
172
+ ```
173
+
174
+ ### 2. Supported Media Types
175
+ The system automatically classifies media based on the `contentType`:
176
+ - `image/*` -> `image`
177
+ - `video/*` -> `video`
178
+ - `audio/*` -> `audio`
179
+ - `application/pdf`, `.docx`, etc. -> `document`
180
+ - Others -> `file`
181
+
129
182
  ## License
130
183
 
131
184
  MIT
package/dist/context.d.ts CHANGED
@@ -1,10 +1,28 @@
1
1
  import React, { ReactNode } from 'react';
2
2
  import { QueryClient } from '@tanstack/react-query';
3
3
  import { PHCMSClient } from './client';
4
+ import { UserDto } from '@ph-cms/api-contract';
5
+ export interface PHCMSContextType {
6
+ client: PHCMSClient;
7
+ user: UserDto | null;
8
+ isAuthenticated: boolean;
9
+ isLoading: boolean;
10
+ }
4
11
  export interface PHCMSProviderProps {
5
12
  client: PHCMSClient;
6
13
  queryClient?: QueryClient;
7
14
  children: ReactNode;
8
15
  }
16
+ /**
17
+ * Root Provider for PH-CMS
18
+ * Automatically includes QueryClientProvider
19
+ */
9
20
  export declare const PHCMSProvider: React.FC<PHCMSProviderProps>;
21
+ /**
22
+ * Access the full PH-CMS Context (client, user, auth status)
23
+ */
24
+ export declare const usePHCMSContext: () => PHCMSContextType;
25
+ /**
26
+ * Access the PH-CMS Client instance
27
+ */
10
28
  export declare const usePHCMS: () => PHCMSClient;
package/dist/context.js CHANGED
@@ -33,21 +33,60 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.usePHCMS = exports.PHCMSProvider = void 0;
36
+ exports.usePHCMS = exports.usePHCMSContext = exports.PHCMSProvider = void 0;
37
37
  const react_1 = __importStar(require("react"));
38
38
  const react_query_1 = require("@tanstack/react-query");
39
39
  const PHCMSContext = (0, react_1.createContext)(null);
40
+ /**
41
+ * Internal component to handle auth state and provide to context
42
+ */
43
+ const PHCMSInternalProvider = ({ client, children }) => {
44
+ const { data: user, isLoading, isError } = (0, react_query_1.useQuery)({
45
+ queryKey: ['auth', 'me'],
46
+ queryFn: () => client.auth.me(),
47
+ retry: false,
48
+ staleTime: 1000 * 60 * 5,
49
+ });
50
+ const value = (0, react_1.useMemo)(() => ({
51
+ client,
52
+ user: user || null,
53
+ isAuthenticated: !!user && !isError,
54
+ isLoading,
55
+ }), [client, user, isError, isLoading]);
56
+ return (react_1.default.createElement(PHCMSContext.Provider, { value: value }, children));
57
+ };
58
+ /**
59
+ * Root Provider for PH-CMS
60
+ * Automatically includes QueryClientProvider
61
+ */
40
62
  const PHCMSProvider = ({ client, queryClient, children }) => {
41
- const internalQueryClient = (0, react_1.useMemo)(() => queryClient ?? new react_query_1.QueryClient(), [queryClient]);
42
- return (react_1.default.createElement(PHCMSContext.Provider, { value: { client } },
43
- react_1.default.createElement(react_query_1.QueryClientProvider, { client: internalQueryClient }, children)));
63
+ const internalQueryClient = (0, react_1.useMemo)(() => queryClient ?? new react_query_1.QueryClient({
64
+ defaultOptions: {
65
+ queries: {
66
+ refetchOnWindowFocus: false,
67
+ retry: false,
68
+ },
69
+ },
70
+ }), [queryClient]);
71
+ return (react_1.default.createElement(react_query_1.QueryClientProvider, { client: internalQueryClient },
72
+ react_1.default.createElement(PHCMSInternalProvider, { client: client }, children)));
44
73
  };
45
74
  exports.PHCMSProvider = PHCMSProvider;
46
- const usePHCMS = () => {
75
+ /**
76
+ * Access the full PH-CMS Context (client, user, auth status)
77
+ */
78
+ const usePHCMSContext = () => {
47
79
  const context = (0, react_1.useContext)(PHCMSContext);
48
80
  if (!context) {
49
- throw new Error('usePHCMS must be used within a PHCMSProvider');
81
+ throw new Error('usePHCMSContext must be used within a PHCMSProvider');
50
82
  }
51
- return context.client;
83
+ return context;
84
+ };
85
+ exports.usePHCMSContext = usePHCMSContext;
86
+ /**
87
+ * Access the PH-CMS Client instance
88
+ */
89
+ const usePHCMS = () => {
90
+ return (0, exports.usePHCMSContext)().client;
52
91
  };
53
92
  exports.usePHCMS = usePHCMS;
@@ -1,5 +1,8 @@
1
- export declare const useLogin: () => import("@tanstack/react-query").UseMutationResult<{
2
- refreshToken: string;
1
+ /**
2
+ * Unified Auth Hook
3
+ * Returns both the current auth status and the actions
4
+ */
5
+ export declare const useAuth: () => {
3
6
  user: {
4
7
  uid: string;
5
8
  email: string;
@@ -17,39 +20,148 @@ export declare const useLogin: () => import("@tanstack/react-query").UseMutation
17
20
  last_login_at: string | null;
18
21
  created_at: string;
19
22
  updated_at: string;
20
- };
21
- accessToken: string;
22
- }, Error, {
23
- email: string;
24
- password: string;
25
- }, unknown>;
26
- export declare const useLoginWithFirebase: () => import("@tanstack/react-query").UseMutationResult<{
27
- refreshToken: string;
28
- user: {
29
- uid: string;
23
+ } | null;
24
+ isAuthenticated: boolean;
25
+ isLoading: boolean;
26
+ login: import("@tanstack/react-query").UseMutateAsyncFunction<{
27
+ refreshToken: string;
28
+ user: {
29
+ uid: string;
30
+ email: string;
31
+ username: string | null;
32
+ display_name: string;
33
+ avatar_url: string | null;
34
+ phone_number: string | null;
35
+ email_verified_at: string | null;
36
+ phone_verified_at: string | null;
37
+ locale: string;
38
+ timezone: string;
39
+ status: string;
40
+ role: string[];
41
+ profile_data: Record<string, any>;
42
+ last_login_at: string | null;
43
+ created_at: string;
44
+ updated_at: string;
45
+ };
46
+ accessToken: string;
47
+ }, Error, {
48
+ email: string;
49
+ password: string;
50
+ }, unknown>;
51
+ loginWithFirebase: import("@tanstack/react-query").UseMutateAsyncFunction<{
52
+ refreshToken: string;
53
+ user: {
54
+ uid: string;
55
+ email: string;
56
+ username: string | null;
57
+ display_name: string;
58
+ avatar_url: string | null;
59
+ phone_number: string | null;
60
+ email_verified_at: string | null;
61
+ phone_verified_at: string | null;
62
+ locale: string;
63
+ timezone: string;
64
+ status: string;
65
+ role: string[];
66
+ profile_data: Record<string, any>;
67
+ last_login_at: string | null;
68
+ created_at: string;
69
+ updated_at: string;
70
+ };
71
+ accessToken: string;
72
+ }, Error, {
73
+ idToken: string;
74
+ }, unknown>;
75
+ register: import("@tanstack/react-query").UseMutateAsyncFunction<{
76
+ refreshToken: string;
77
+ user: {
78
+ uid: string;
79
+ email: string;
80
+ username: string | null;
81
+ display_name: string;
82
+ avatar_url: string | null;
83
+ phone_number: string | null;
84
+ email_verified_at: string | null;
85
+ phone_verified_at: string | null;
86
+ locale: string;
87
+ timezone: string;
88
+ status: string;
89
+ role: string[];
90
+ profile_data: Record<string, any>;
91
+ last_login_at: string | null;
92
+ created_at: string;
93
+ updated_at: string;
94
+ };
95
+ accessToken: string;
96
+ }, Error, {
30
97
  email: string;
31
- username: string | null;
32
98
  display_name: string;
33
- avatar_url: string | null;
34
- phone_number: string | null;
35
- email_verified_at: string | null;
36
- phone_verified_at: string | null;
37
- locale: string;
38
- timezone: string;
39
- status: string;
40
- role: string[];
41
- profile_data: Record<string, any>;
42
- last_login_at: string | null;
43
- created_at: string;
44
- updated_at: string;
45
- };
46
- accessToken: string;
47
- }, Error, {
48
- idToken: string;
49
- }, unknown>;
50
- export declare const useRegister: () => import("@tanstack/react-query").UseMutationResult<{
51
- refreshToken: string;
52
- user: {
99
+ username?: string | null | undefined;
100
+ password?: string | undefined;
101
+ provider?: string | undefined;
102
+ provider_subject?: string | undefined;
103
+ termCodes?: string[] | undefined;
104
+ }, unknown>;
105
+ logout: import("@tanstack/react-query").UseMutateAsyncFunction<void, Error, void, unknown>;
106
+ loginStatus: import("@tanstack/react-query").UseMutationResult<{
107
+ refreshToken: string;
108
+ user: {
109
+ uid: string;
110
+ email: string;
111
+ username: string | null;
112
+ display_name: string;
113
+ avatar_url: string | null;
114
+ phone_number: string | null;
115
+ email_verified_at: string | null;
116
+ phone_verified_at: string | null;
117
+ locale: string;
118
+ timezone: string;
119
+ status: string;
120
+ role: string[];
121
+ profile_data: Record<string, any>;
122
+ last_login_at: string | null;
123
+ created_at: string;
124
+ updated_at: string;
125
+ };
126
+ accessToken: string;
127
+ }, Error, {
128
+ email: string;
129
+ password: string;
130
+ }, unknown>;
131
+ registerStatus: import("@tanstack/react-query").UseMutationResult<{
132
+ refreshToken: string;
133
+ user: {
134
+ uid: string;
135
+ email: string;
136
+ username: string | null;
137
+ display_name: string;
138
+ avatar_url: string | null;
139
+ phone_number: string | null;
140
+ email_verified_at: string | null;
141
+ phone_verified_at: string | null;
142
+ locale: string;
143
+ timezone: string;
144
+ status: string;
145
+ role: string[];
146
+ profile_data: Record<string, any>;
147
+ last_login_at: string | null;
148
+ created_at: string;
149
+ updated_at: string;
150
+ };
151
+ accessToken: string;
152
+ }, Error, {
153
+ email: string;
154
+ display_name: string;
155
+ username?: string | null | undefined;
156
+ password?: string | undefined;
157
+ provider?: string | undefined;
158
+ provider_subject?: string | undefined;
159
+ termCodes?: string[] | undefined;
160
+ }, unknown>;
161
+ logoutStatus: import("@tanstack/react-query").UseMutationResult<void, Error, void, unknown>;
162
+ };
163
+ export declare const useUser: () => {
164
+ data: {
53
165
  uid: string;
54
166
  email: string;
55
167
  username: string | null;
@@ -66,33 +178,37 @@ export declare const useRegister: () => import("@tanstack/react-query").UseMutat
66
178
  last_login_at: string | null;
67
179
  created_at: string;
68
180
  updated_at: string;
69
- };
70
- accessToken: string;
71
- }, Error, {
72
- email: string;
73
- display_name: string;
74
- username?: string | null | undefined;
75
- password?: string | undefined;
76
- provider?: string | undefined;
77
- provider_subject?: string | undefined;
78
- termCodes?: string[] | undefined;
79
- }, unknown>;
80
- export declare const useLogout: () => import("@tanstack/react-query").UseMutationResult<void, Error, void, unknown>;
81
- export declare const useUser: () => import("@tanstack/react-query").UseQueryResult<{
82
- uid: string;
83
- email: string;
84
- username: string | null;
85
- display_name: string;
86
- avatar_url: string | null;
87
- phone_number: string | null;
88
- email_verified_at: string | null;
89
- phone_verified_at: string | null;
90
- locale: string;
91
- timezone: string;
92
- status: string;
93
- role: string[];
94
- profile_data: Record<string, any>;
95
- last_login_at: string | null;
96
- created_at: string;
97
- updated_at: string;
98
- }, Error>;
181
+ } | null;
182
+ isLoading: boolean;
183
+ isAuthenticated: boolean;
184
+ };
185
+ export declare const useLogin: () => {
186
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<{
187
+ refreshToken: string;
188
+ user: {
189
+ uid: string;
190
+ email: string;
191
+ username: string | null;
192
+ display_name: string;
193
+ avatar_url: string | null;
194
+ phone_number: string | null;
195
+ email_verified_at: string | null;
196
+ phone_verified_at: string | null;
197
+ locale: string;
198
+ timezone: string;
199
+ status: string;
200
+ role: string[];
201
+ profile_data: Record<string, any>;
202
+ last_login_at: string | null;
203
+ created_at: string;
204
+ updated_at: string;
205
+ };
206
+ accessToken: string;
207
+ }, Error, {
208
+ email: string;
209
+ password: string;
210
+ }, unknown>;
211
+ };
212
+ export declare const useLogout: () => {
213
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<void, Error, void, unknown>;
214
+ };
@@ -1,62 +1,69 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useUser = exports.useLogout = exports.useRegister = exports.useLoginWithFirebase = exports.useLogin = void 0;
3
+ exports.useLogout = exports.useLogin = exports.useUser = exports.useAuth = void 0;
4
4
  const react_query_1 = require("@tanstack/react-query");
5
5
  const context_1 = require("../context");
6
- const useLogin = () => {
6
+ /**
7
+ * Unified Auth Hook
8
+ * Returns both the current auth status and the actions
9
+ */
10
+ const useAuth = () => {
11
+ const { user, isAuthenticated, isLoading } = (0, context_1.usePHCMSContext)();
7
12
  const client = (0, context_1.usePHCMS)();
8
13
  const queryClient = (0, react_query_1.useQueryClient)();
9
- return (0, react_query_1.useMutation)({
14
+ const loginMutation = (0, react_query_1.useMutation)({
10
15
  mutationFn: (data) => client.auth.login(data),
11
- onSuccess: (data) => {
12
- // Invalidate user query to fetch fresh user data
16
+ onSuccess: () => {
13
17
  queryClient.invalidateQueries({ queryKey: ['auth', 'me'] });
14
18
  },
15
19
  });
16
- };
17
- exports.useLogin = useLogin;
18
- const useLoginWithFirebase = () => {
19
- const client = (0, context_1.usePHCMS)();
20
- const queryClient = (0, react_query_1.useQueryClient)();
21
- return (0, react_query_1.useMutation)({
20
+ const loginWithFirebaseMutation = (0, react_query_1.useMutation)({
22
21
  mutationFn: (data) => client.auth.loginWithFirebase(data),
23
- onSuccess: (data) => {
24
- // Invalidate user query to fetch fresh user data
22
+ onSuccess: () => {
25
23
  queryClient.invalidateQueries({ queryKey: ['auth', 'me'] });
26
24
  },
27
25
  });
28
- };
29
- exports.useLoginWithFirebase = useLoginWithFirebase;
30
- const useRegister = () => {
31
- const client = (0, context_1.usePHCMS)();
32
- const queryClient = (0, react_query_1.useQueryClient)();
33
- return (0, react_query_1.useMutation)({
26
+ const registerMutation = (0, react_query_1.useMutation)({
34
27
  mutationFn: (data) => client.auth.register(data),
35
28
  onSuccess: () => {
36
29
  queryClient.invalidateQueries({ queryKey: ['auth', 'me'] });
37
30
  },
38
31
  });
39
- };
40
- exports.useRegister = useRegister;
41
- const useLogout = () => {
42
- const client = (0, context_1.usePHCMS)();
43
- const queryClient = (0, react_query_1.useQueryClient)();
44
- return (0, react_query_1.useMutation)({
32
+ const logoutMutation = (0, react_query_1.useMutation)({
45
33
  mutationFn: () => client.auth.logout(),
46
34
  onSuccess: () => {
47
35
  queryClient.setQueryData(['auth', 'me'], null);
48
36
  queryClient.invalidateQueries();
49
37
  },
50
38
  });
39
+ return {
40
+ user,
41
+ isAuthenticated,
42
+ isLoading,
43
+ login: loginMutation.mutateAsync,
44
+ loginWithFirebase: loginWithFirebaseMutation.mutateAsync,
45
+ register: registerMutation.mutateAsync,
46
+ logout: logoutMutation.mutateAsync,
47
+ // Access to mutation objects for loading/error states if needed
48
+ loginStatus: loginMutation,
49
+ registerStatus: registerMutation,
50
+ logoutStatus: logoutMutation,
51
+ };
51
52
  };
52
- exports.useLogout = useLogout;
53
+ exports.useAuth = useAuth;
54
+ // Keep individual hooks for backward compatibility or more specific use cases
53
55
  const useUser = () => {
54
- const client = (0, context_1.usePHCMS)();
55
- return (0, react_query_1.useQuery)({
56
- queryKey: ['auth', 'me'],
57
- queryFn: () => client.auth.me(),
58
- retry: false, // Don't retry if 401
59
- staleTime: 1000 * 60 * 5, // 5 minutes
60
- });
56
+ const { user, isLoading, isAuthenticated } = (0, context_1.usePHCMSContext)();
57
+ return { data: user, isLoading, isAuthenticated };
61
58
  };
62
59
  exports.useUser = useUser;
60
+ const useLogin = () => {
61
+ const { login } = (0, exports.useAuth)();
62
+ return { mutateAsync: login };
63
+ };
64
+ exports.useLogin = useLogin;
65
+ const useLogout = () => {
66
+ const { logout } = (0, exports.useAuth)();
67
+ return { mutateAsync: logout };
68
+ };
69
+ exports.useLogout = useLogout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ph-cms/client-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Unified PH-CMS Client SDK (React + Core)",
5
5
  "keywords": [],
6
6
  "license": "MIT",