kodzero-front-sdk-alfa 0.0.6 → 0.0.8

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.
@@ -6,18 +6,48 @@ 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
- login: (input: KodzeroAuthEmailLogin) => 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
- register: (userData: Record<string, string>) => Promise<Record<string, any>>;
19
- verify: () => Promise<any>;
20
- refresh: () => Promise<any>;
21
- logout: () => Promise<any>;
51
+ logout: () => Promise<boolean>;
22
52
  }
23
53
  export default KodzeroAuthEmail;
@@ -19,10 +19,10 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
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
27
  register = async (userData) => {
28
28
  const url = buildURL(this.host, this.collection + '/register');
@@ -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,10 +53,10 @@ 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
61
  logout = async () => {
62
62
  const url = buildURL(this.host, this.collection + '/logout');
@@ -15,7 +15,7 @@ const createModel = (options, apiClient) => {
15
15
  if (!id)
16
16
  throw new Error(constants.RequiresId);
17
17
  const getUrl = buildURL(Model.host, Model.collection, id);
18
- const response = await Model.api.get(getUrl);
18
+ const response = await Model.apiClient.get(getUrl);
19
19
  await Model._handleApiError(response);
20
20
  const json = await response.json();
21
21
  return new Model(json.result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kodzero-front-sdk-alfa",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "tsc": "tsc -b src/",
package/src/auth/email.ts CHANGED
@@ -9,6 +9,42 @@ interface KodzeroAuthEmailLogin {
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"
@@ -32,48 +68,48 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
32
68
  /**
33
69
  * Login with email and password. On success, sets tokens in TokensManager automatically
34
70
  */
35
- login = async (input: KodzeroAuthEmailLogin): Promise<{access: string, refresh: string}> => {
71
+ login = async (input: KodzeroAuthEmailLogin): Promise<LoginResponse['result']> => {
36
72
  const url = buildURL(this.host, this.collection + '/login')
37
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
88
  * Register with email and password. On success, sets tokens in TokensManager automatically
53
89
  */
54
- register = async (userData: Record<string, string>): Promise<Record<string, any>> => {
90
+ register = async (userData: Record<string, string>): Promise<RegisterResponse['result']> => {
55
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
141
  * Logout the user
106
142
  * If success, clears tokens in TokensManager automatically
107
143
  */
108
- logout = async (): Promise<any> => {
144
+ logout = async (): Promise<boolean> => {
109
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
 
@@ -36,7 +36,7 @@ const createModel = <
36
36
 
37
37
  const getUrl = buildURL(Model.host, Model.collection, id)
38
38
 
39
- const response = await Model.api.get(getUrl);
39
+ const response = await Model.apiClient.get(getUrl);
40
40
  await Model._handleApiError(response);
41
41
 
42
42
  const json = await response.json();