kodzero-front-sdk-alfa 0.0.5 → 0.0.7

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
@@ -18,14 +18,14 @@ const kodzero = new Kodzero({
18
18
  host: 'https://api.your-backend.com'
19
19
  });
20
20
 
21
- // Sign up a new user
22
- const signupResult = await kodzero.auth.signup({
21
+ // New user registration
22
+ const registerResult = await kodzero.auth.register({
23
23
  email: 'user@example.com',
24
24
  password: 'secure_password'
25
25
  });
26
26
 
27
- // Sign in
28
- const signinResult = await kodzero.auth.signin({
27
+ // Login
28
+ const loginResult = await kodzero.auth.login({
29
29
  email: 'user@example.com',
30
30
  password: 'secure_password'
31
31
  });
@@ -36,8 +36,8 @@ const verified = await kodzero.auth.verify();
36
36
  // Refresh the token
37
37
  const refreshed = await kodzero.auth.refresh();
38
38
 
39
- // Sign out
40
- await kodzero.auth.signout();
39
+ // Logout
40
+ await kodzero.auth.logout();
41
41
 
42
42
  // Manually set tokens (useful for persisting sessions)
43
43
  kodzero.auth.setTokens('access_token', 'refresh_token');
@@ -7,10 +7,10 @@ declare class KodzeroAuthBase {
7
7
  tokensManager: TokensManagerClass;
8
8
  constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass);
9
9
  _handleApiError(response: Response): Promise<void>;
10
- signin: (...args: any[]) => Promise<any> | void;
11
- signup: (...args: any[]) => Promise<any> | void;
10
+ login: (...args: any[]) => Promise<any> | void;
11
+ register: (...args: any[]) => Promise<any> | void;
12
12
  refresh: (...args: any[]) => Promise<any> | void;
13
- signout: (...args: any[]) => Promise<any> | void;
13
+ logout: (...args: any[]) => Promise<any> | void;
14
14
  verify: (...args: any[]) => Promise<any> | void;
15
15
  }
16
16
  export default KodzeroAuthBase;
package/dist/auth/base.js CHANGED
@@ -13,10 +13,10 @@ class KodzeroAuthBase {
13
13
  _handleApiError(response) {
14
14
  return validateApiResponse(response);
15
15
  }
16
- signin = (...args) => { };
17
- signup = (...args) => { };
16
+ login = (...args) => { };
17
+ register = (...args) => { };
18
18
  refresh = (...args) => { };
19
- signout = (...args) => { };
19
+ logout = (...args) => { };
20
20
  verify = (...args) => { };
21
21
  }
22
22
  export default KodzeroAuthBase;
@@ -2,22 +2,52 @@ import FluidFetch from "fluid-fetch";
2
2
  import { AuthOptions } from "./index.js";
3
3
  import KodzeroAuthBase from "./base.js";
4
4
  import TokensManagerClass from "./tokens.js";
5
- interface KodzeroAuthEmailSignin {
5
+ interface KodzeroAuthEmailLogin {
6
6
  email: string;
7
7
  password: string;
8
8
  }
9
+ interface SuccessResponse<Result> {
10
+ ok: true;
11
+ result: Result;
12
+ }
13
+ interface Tokens {
14
+ access: string;
15
+ refresh: string;
16
+ }
17
+ interface AuthUser {
18
+ _id: string;
19
+ email: string;
20
+ name: string;
21
+ workspace: string;
22
+ createdAt: string;
23
+ updatedAt: string;
24
+ [key: string]: any;
25
+ }
26
+ type RegisterResponse = SuccessResponse<{
27
+ user: AuthUser;
28
+ tokens: Tokens;
29
+ session: number;
30
+ }>;
31
+ type LoginResponse = SuccessResponse<{
32
+ user: AuthUser;
33
+ tokens: Tokens;
34
+ session: number;
35
+ }>;
36
+ type VerifyResponse = SuccessResponse<{}>;
37
+ type RefreshResponse = SuccessResponse<{
38
+ tokens: Tokens;
39
+ }>;
9
40
  declare class KodzeroAuthEmail extends KodzeroAuthBase {
10
41
  tokensManager: TokensManagerClass;
11
42
  collection: "auth/password";
12
43
  constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass);
13
44
  _setTokens: (access: string, refresh?: string) => void;
14
- signin: (input: KodzeroAuthEmailSignin) => Promise<{
15
- access: string;
16
- refresh: string;
45
+ login: (input: KodzeroAuthEmailLogin) => Promise<LoginResponse["result"]>;
46
+ register: (userData: Record<string, string>) => Promise<RegisterResponse["result"]>;
47
+ verify: () => Promise<VerifyResponse["result"]>;
48
+ refresh: () => Promise<RefreshResponse["result"] | {
49
+ tokens: null;
17
50
  }>;
18
- signup: (userData: Record<string, string>) => Promise<Record<string, any>>;
19
- verify: () => Promise<any>;
20
- refresh: () => Promise<any>;
21
- signout: () => Promise<any>;
51
+ logout: () => Promise<boolean>;
22
52
  }
23
53
  export default KodzeroAuthEmail;
@@ -13,19 +13,19 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
13
13
  if (refresh)
14
14
  this.tokensManager.setRefresh(refresh);
15
15
  };
16
- signin = async (input) => {
17
- const url = buildURL(this.host, this.collection + '/signin');
16
+ login = async (input) => {
17
+ const url = buildURL(this.host, this.collection + '/login');
18
18
  const response = await this.api.post(url, input)
19
19
  .headers({ 'Content-Type': 'application/json' });
20
20
  await this._handleApiError(response);
21
21
  const json = await response.json();
22
- if (json.ok && json.tokens && json.tokens.access && json.tokens.refresh) {
23
- this._setTokens(json.tokens.access, json.tokens.refresh);
22
+ if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
23
+ this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
24
24
  }
25
- return json.tokens;
25
+ return json.result;
26
26
  };
27
- signup = async (userData) => {
28
- const url = buildURL(this.host, this.collection + '/signup');
27
+ register = async (userData) => {
28
+ const url = buildURL(this.host, this.collection + '/register');
29
29
  const response = await this.api.post(url, userData)
30
30
  .headers({ 'Content-Type': 'application/json' });
31
31
  await this._handleApiError(response);
@@ -33,7 +33,7 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
33
33
  if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
34
34
  this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
35
35
  }
36
- return json.result.user;
36
+ return json.result;
37
37
  };
38
38
  verify = async () => {
39
39
  try {
@@ -53,13 +53,13 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
53
53
  .headers({ 'Content-Type': 'application/json' });
54
54
  await this._handleApiError(response);
55
55
  const json = await response.json();
56
- if (json.ok && json.tokens && json.tokens.access) {
57
- this.tokensManager.setAccess(json.tokens.access);
56
+ if (json.ok && json.result?.tokens && json.result.tokens.access) {
57
+ this.tokensManager.setAccess(json.result.tokens.access);
58
58
  }
59
- return json.ok ? true : false;
59
+ return json.ok ? { tokens: json.result.tokens } : { tokens: null };
60
60
  };
61
- signout = async () => {
62
- const url = buildURL(this.host, this.collection + '/signout');
61
+ logout = async () => {
62
+ const url = buildURL(this.host, this.collection + '/logout');
63
63
  const response = await this.api.post(url, {})
64
64
  .headers({ 'Content-Type': 'application/json' });
65
65
  await this._handleApiError(response);
@@ -7,9 +7,9 @@ class KodzeroAuth extends KodzeroAuthBase {
7
7
  constructor(options, api, tokensManager) {
8
8
  super(options, api, tokensManager);
9
9
  this.email = new KodzeroAuthEmail(options, api, tokensManager);
10
- this.signin = this.email.signin;
11
- this.signup = this.email.signup;
12
- this.signout = this.email.signout;
10
+ this.login = this.email.login;
11
+ this.register = this.email.register;
12
+ this.logout = this.email.logout;
13
13
  this.verify = this.email.verify;
14
14
  this.refresh = this.email.refresh;
15
15
  this.setTokens = (access, refresh) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kodzero-front-sdk-alfa",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "tsc": "tsc -b src/",
package/src/auth/base.ts CHANGED
@@ -29,10 +29,10 @@ class KodzeroAuthBase {
29
29
  * Base auth methods.
30
30
  * These will be overridden by specific strategies (e.g. email, social, etc.)
31
31
  */
32
- signin = (...args: any[]): Promise<any> | void => {}
33
- signup = (...args: any[]): Promise<any> | void => {}
32
+ login = (...args: any[]): Promise<any> | void => {}
33
+ register = (...args: any[]): Promise<any> | void => {}
34
34
  refresh = (...args: any[]): Promise<any> | void => {}
35
- signout = (...args: any[]): Promise<any> | void => {}
35
+ logout = (...args: any[]): Promise<any> | void => {}
36
36
  verify = (...args: any[]): Promise<any> | void => {}
37
37
  }
38
38
 
package/src/auth/email.ts CHANGED
@@ -4,11 +4,47 @@ import buildURL from "../utils/buildURL.js"
4
4
  import KodzeroAuthBase from "./base.js"
5
5
  import TokensManagerClass from "./tokens.js"
6
6
 
7
- interface KodzeroAuthEmailSignin {
7
+ interface KodzeroAuthEmailLogin {
8
8
  email: string
9
9
  password: string
10
10
  }
11
11
 
12
+ interface SuccessResponse<Result> {
13
+ ok: true
14
+ result: Result
15
+ }
16
+
17
+ interface Tokens {
18
+ access: string
19
+ refresh: string
20
+ }
21
+
22
+ interface AuthUser {
23
+ _id: string
24
+ email: string
25
+ name: string
26
+ workspace: string
27
+ createdAt: string
28
+ updatedAt: string
29
+ [key: string]: any
30
+ }
31
+
32
+ type RegisterResponse = SuccessResponse<{
33
+ user: AuthUser
34
+ tokens: Tokens
35
+ session: number
36
+ }>
37
+
38
+ type LoginResponse = SuccessResponse<{
39
+ user: AuthUser
40
+ tokens: Tokens
41
+ session: number
42
+ }>
43
+
44
+ type VerifyResponse = SuccessResponse<{}>
45
+ type RefreshResponse = SuccessResponse<{tokens: Tokens}>
46
+ type LogoutResponse = SuccessResponse<boolean>
47
+
12
48
  class KodzeroAuthEmail extends KodzeroAuthBase {
13
49
  tokensManager: TokensManagerClass
14
50
  collection: "auth/password"
@@ -30,50 +66,50 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
30
66
  }
31
67
 
32
68
  /**
33
- * Sign in with email and password. On success, sets tokens in TokensManager automatically
69
+ * Login with email and password. On success, sets tokens in TokensManager automatically
34
70
  */
35
- signin = async (input: KodzeroAuthEmailSignin): Promise<{access: string, refresh: string}> => {
36
- const url = buildURL(this.host, this.collection + '/signin')
37
-
71
+ login = async (input: KodzeroAuthEmailLogin): Promise<LoginResponse['result']> => {
72
+ const url = buildURL(this.host, this.collection + '/login')
73
+
38
74
  const response = await this.api.post(url, input)
39
75
  .headers({ 'Content-Type': 'application/json' });
40
76
 
41
77
  await this._handleApiError(response);
42
- const json = await response.json();
78
+ const json: LoginResponse = await response.json();
43
79
 
44
- if (json.ok && json.tokens && json.tokens.access && json.tokens.refresh) {
45
- this._setTokens(json.tokens.access, json.tokens.refresh);
80
+ if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
81
+ this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
46
82
  }
47
83
 
48
- return json.tokens
84
+ return json.result
49
85
  }
50
86
 
51
87
  /**
52
- * Sign up with email and password. On success, sets tokens in TokensManager automatically
88
+ * Register with email and password. On success, sets tokens in TokensManager automatically
53
89
  */
54
- signup = async (userData: Record<string, string>): Promise<Record<string, any>> => {
55
- const url = buildURL(this.host, this.collection + '/signup')
90
+ register = async (userData: Record<string, string>): Promise<RegisterResponse['result']> => {
91
+ const url = buildURL(this.host, this.collection + '/register')
56
92
  const response = await this.api.post(url, userData)
57
93
  .headers({ 'Content-Type': 'application/json' });
58
94
 
59
95
  await this._handleApiError(response);
60
- const json = await response.json();
96
+ const json: RegisterResponse = await response.json();
61
97
 
62
98
  if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
63
99
  this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
64
100
  }
65
101
 
66
- return json.result.user
102
+ return json.result
67
103
  }
68
104
 
69
105
  /**
70
106
  * Verify current access token
71
107
  */
72
- verify = async (): Promise<any> => {
108
+ verify = async (): Promise<VerifyResponse['result']> => {
73
109
  try {
74
110
  const url = buildURL(this.host, this.collection + '/verify')
75
111
  const response = await this.api.get(url)
76
- const json = await response.json();
112
+ const json: VerifyResponse = await response.json();
77
113
 
78
114
  return json.ok ? true : false
79
115
  } catch (error) {
@@ -86,32 +122,32 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
86
122
  * Refresh access token using refresh token
87
123
  * If success, updates access token in TokensManager automatically
88
124
  */
89
- refresh = async (): Promise<any> => {
125
+ refresh = async (): Promise<RefreshResponse['result'] | {tokens: null}> => {
90
126
  const url = buildURL(this.host, this.collection + '/refresh')
91
127
  const response = await this.api.post(url, { refresh: this.tokensManager.refresh })
92
128
  .headers({ 'Content-Type': 'application/json' });
93
129
 
94
130
  await this._handleApiError(response);
95
- const json = await response.json();
131
+ const json: RefreshResponse = await response.json();
96
132
 
97
- if (json.ok && json.tokens && json.tokens.access) {
98
- this.tokensManager.setAccess(json.tokens.access)
133
+ if (json.ok && json.result?.tokens && json.result.tokens.access) {
134
+ this.tokensManager.setAccess(json.result.tokens.access)
99
135
  }
100
136
 
101
- return json.ok ? true : false
137
+ return json.ok ? { tokens: json.result.tokens } : { tokens: null }
102
138
  }
103
139
 
104
140
  /**
105
- * Sign out the user
141
+ * Logout the user
106
142
  * If success, clears tokens in TokensManager automatically
107
143
  */
108
- signout = async (): Promise<any> => {
109
- const url = buildURL(this.host, this.collection + '/signout')
144
+ logout = async (): Promise<boolean> => {
145
+ const url = buildURL(this.host, this.collection + '/logout')
110
146
  const response = await this.api.post(url, {})
111
147
  .headers({ 'Content-Type': 'application/json' });
112
148
 
113
149
  await this._handleApiError(response);
114
- const json = await response.json();
150
+ const json: LogoutResponse = await response.json();
115
151
 
116
152
  if (json.ok) this.tokensManager.clear()
117
153
 
package/src/auth/index.ts CHANGED
@@ -19,9 +19,9 @@ class KodzeroAuth extends KodzeroAuthBase {
19
19
  this.email = new KodzeroAuthEmail(options, api, tokensManager)
20
20
 
21
21
  // Default methods set to email strategy methods
22
- this.signin = this.email.signin
23
- this.signup = this.email.signup
24
- this.signout = this.email.signout
22
+ this.login = this.email.login
23
+ this.register = this.email.register
24
+ this.logout = this.email.logout
25
25
  this.verify = this.email.verify
26
26
  this.refresh = this.email.refresh
27
27