moc-oauth-client 1.0.2 → 1.0.4

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/dist/index.d.ts CHANGED
@@ -1,17 +1,12 @@
1
- interface OAuthSuccess<T> {
2
- statusCode: number;
3
- success: true;
4
- message: string;
5
- data: T;
1
+ interface OAuthResult<T> {
2
+ data: T | null;
3
+ error: OAuthError["error"] | null;
6
4
  }
7
5
  interface OAuthError {
8
6
  success: false;
9
7
  error: {
10
8
  code: string;
11
9
  message: string;
12
- timestamp: string;
13
- path: string;
14
- requestId: string;
15
10
  };
16
11
  }
17
12
  interface AuthorizeResponse {
@@ -29,44 +24,43 @@ interface CurrentUser {
29
24
  }
30
25
 
31
26
  /**
32
- * MocOAuthClient is a client library for interacting with the Moc OAuth API.
27
+ * MOCOAuthClient is a client library for interacting with the MOC OAuth API.
33
28
  * It provides methods for authorizing clients, validating tokens, refreshing tokens,
34
29
  * and retrieving the current user's information.
35
30
  */
36
- declare class MocOAuthClient {
31
+ declare class MOCOAuthClient {
37
32
  /**
38
- * Authorize a client using the OAuth API.
33
+ * Authorizes a client using the MOC OAuth API.
39
34
  * @returns A promise resolving to an object containing the response data.
40
- * The response data will contain a redirectUri which can be used to redirect the user to the Moc OAuth API's authorization page.
41
- * @throws An error if the authorization request fails.
35
+ * The response data will contain the authorization URL for the client.
36
+ * @throws An error if the request to authorize the client fails.
42
37
  */
43
- authorizeClient(): Promise<OAuthSuccess<AuthorizeResponse>>;
38
+ authorizeClient(): Promise<OAuthResult<AuthorizeResponse>>;
44
39
  /**
45
- * Validates a JWT token using the Moc OAuth API.
40
+ * Validates a JWT token using the MOC OAuth API.
46
41
  * @param accessToken The JWT token to validate.
47
42
  * @returns A promise resolving to an object containing the response data.
48
43
  * The response data will contain a boolean indicating whether the token is valid or not,
49
44
  * an access token, and a refresh token.
50
- * @throws An error if the validation request fails.
45
+ * @throws An error if the request to validate the token fails.
51
46
  */
52
- validateToken(accessToken: string): Promise<OAuthSuccess<ValidateTokenResponse>>;
47
+ validateToken(accessToken: string): Promise<OAuthResult<ValidateTokenResponse>>;
53
48
  /**
54
- * Retrieves the current user's information using the Moc OAuth API.
49
+ * Retrieves the current user's information using the MOC OAuth API.
55
50
  * @param accessToken The JWT token to use for authentication.
56
51
  * @returns A promise resolving to an object containing the response data.
57
52
  * The response data will contain the current user's email, first name, and last name.
58
- * @throws An error if the request to retrieve the current user's information fails.
53
+ * @throws An error if the request to retrieve the user's information fails.
59
54
  */
60
- getCurrentUser(accessToken: string): Promise<OAuthSuccess<CurrentUser>>;
55
+ getCurrentUser(accessToken: string): Promise<OAuthResult<CurrentUser>>;
61
56
  /**
62
- * Refreshes a JWT token using the Moc OAuth API.
63
- * @param refreshToken The refresh token to use for authentication.
57
+ * Refreshes an access token using the MOC OAuth API.
58
+ * @param refreshToken The JWT token to use for authentication.
64
59
  * @returns A promise resolving to an object containing the response data.
65
- * The response data will contain a boolean indicating whether the token is valid or not,
66
- * an access token, and a refresh token.
67
- * @throws An error if the request to refresh the token fails.
60
+ * The response data will contain the new access token and refresh token.
61
+ * @throws An error if the request to refresh the access token fails.
68
62
  */
69
- refreshToken(refreshToken: string): Promise<OAuthSuccess<ValidateTokenResponse>>;
63
+ refreshToken(refreshToken: string): Promise<OAuthResult<ValidateTokenResponse>>;
70
64
  }
71
65
 
72
- export { type AuthorizeResponse, type CurrentUser, MocOAuthClient, type OAuthError, type OAuthSuccess, type ValidateTokenResponse };
66
+ export { type AuthorizeResponse, type CurrentUser, MOCOAuthClient, type OAuthError, type OAuthResult, type ValidateTokenResponse };
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- MocOAuthClient: () => MocOAuthClient
33
+ MOCOAuthClient: () => MOCOAuthClient
34
34
  });
35
35
  module.exports = __toCommonJS(index_exports);
36
36
 
@@ -60,20 +60,14 @@ var http = import_axios.default.create({
60
60
  });
61
61
 
62
62
  // src/errors.ts
63
- function handleError(error) {
63
+ function parseError(error) {
64
64
  const err = error;
65
- if (err.response?.data) {
66
- throw err.response.data;
65
+ if (err.response?.data?.error) {
66
+ return err.response.data.error;
67
67
  }
68
- throw {
69
- success: false,
70
- error: {
71
- code: "UNKNOWN_ERROR",
72
- message: err.message,
73
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
74
- path: "",
75
- requestId: ""
76
- }
68
+ return {
69
+ code: "UNKNOWN_ERROR",
70
+ message: err.message ?? "Unknown error"
77
71
  };
78
72
  }
79
73
 
@@ -86,12 +80,12 @@ var API_ENDPOINTS = {
86
80
  };
87
81
 
88
82
  // src/client.ts
89
- var MocOAuthClient = class {
83
+ var MOCOAuthClient = class {
90
84
  /**
91
- * Authorize a client using the OAuth API.
85
+ * Authorizes a client using the MOC OAuth API.
92
86
  * @returns A promise resolving to an object containing the response data.
93
- * The response data will contain a redirectUri which can be used to redirect the user to the Moc OAuth API's authorization page.
94
- * @throws An error if the authorization request fails.
87
+ * The response data will contain the authorization URL for the client.
88
+ * @throws An error if the request to authorize the client fails.
95
89
  */
96
90
  async authorizeClient() {
97
91
  try {
@@ -100,35 +94,47 @@ var MocOAuthClient = class {
100
94
  clientSecret: config.clientSecret,
101
95
  redirectUri: config.redirectUri
102
96
  });
103
- return res.data;
97
+ return {
98
+ data: res.data.data,
99
+ error: null
100
+ };
104
101
  } catch (e) {
105
- handleError(e);
102
+ return {
103
+ data: null,
104
+ error: parseError(e)
105
+ };
106
106
  }
107
107
  }
108
108
  /**
109
- * Validates a JWT token using the Moc OAuth API.
109
+ * Validates a JWT token using the MOC OAuth API.
110
110
  * @param accessToken The JWT token to validate.
111
111
  * @returns A promise resolving to an object containing the response data.
112
112
  * The response data will contain a boolean indicating whether the token is valid or not,
113
113
  * an access token, and a refresh token.
114
- * @throws An error if the validation request fails.
114
+ * @throws An error if the request to validate the token fails.
115
115
  */
116
116
  async validateToken(accessToken) {
117
117
  try {
118
118
  const res = await http.post(API_ENDPOINTS.VALIDATE_TOKEN, {
119
119
  accessToken
120
120
  });
121
- return res.data;
121
+ return {
122
+ data: res.data.data,
123
+ error: null
124
+ };
122
125
  } catch (e) {
123
- handleError(e);
126
+ return {
127
+ data: null,
128
+ error: parseError(e)
129
+ };
124
130
  }
125
131
  }
126
132
  /**
127
- * Retrieves the current user's information using the Moc OAuth API.
133
+ * Retrieves the current user's information using the MOC OAuth API.
128
134
  * @param accessToken The JWT token to use for authentication.
129
135
  * @returns A promise resolving to an object containing the response data.
130
136
  * The response data will contain the current user's email, first name, and last name.
131
- * @throws An error if the request to retrieve the current user's information fails.
137
+ * @throws An error if the request to retrieve the user's information fails.
132
138
  */
133
139
  async getCurrentUser(accessToken) {
134
140
  try {
@@ -137,31 +143,42 @@ var MocOAuthClient = class {
137
143
  Authorization: `Bearer ${accessToken}`
138
144
  }
139
145
  });
140
- return res.data;
146
+ return {
147
+ data: res.data.data,
148
+ error: null
149
+ };
141
150
  } catch (e) {
142
- handleError(e);
151
+ return {
152
+ data: null,
153
+ error: parseError(e)
154
+ };
143
155
  }
144
156
  }
145
157
  /**
146
- * Refreshes a JWT token using the Moc OAuth API.
147
- * @param refreshToken The refresh token to use for authentication.
158
+ * Refreshes an access token using the MOC OAuth API.
159
+ * @param refreshToken The JWT token to use for authentication.
148
160
  * @returns A promise resolving to an object containing the response data.
149
- * The response data will contain a boolean indicating whether the token is valid or not,
150
- * an access token, and a refresh token.
151
- * @throws An error if the request to refresh the token fails.
161
+ * The response data will contain the new access token and refresh token.
162
+ * @throws An error if the request to refresh the access token fails.
152
163
  */
153
164
  async refreshToken(refreshToken) {
154
165
  try {
155
166
  const res = await http.post(API_ENDPOINTS.REFRESH_TOKEN, {
156
167
  refreshToken
157
168
  });
158
- return res.data;
169
+ return {
170
+ data: res.data.data,
171
+ error: null
172
+ };
159
173
  } catch (e) {
160
- handleError(e);
174
+ return {
175
+ data: null,
176
+ error: parseError(e)
177
+ };
161
178
  }
162
179
  }
163
180
  };
164
181
  // Annotate the CommonJS export names for ESM import in node:
165
182
  0 && (module.exports = {
166
- MocOAuthClient
183
+ MOCOAuthClient
167
184
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moc-oauth-client",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "OAuth client for Ministry of Commerce, Cambodia",
5
5
  "author": "Ministry of Commerce, Cambodia",
6
6
  "license": "MIT",