@compassdigital/helper
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -19,2 +19,3 @@ module.exports = { | ||
'import/extensions': 'off', | ||
'import/no-unresolved': 'off', | ||
'prettier/prettier': 'error', | ||
@@ -21,0 +22,0 @@ }, |
@@ -44,3 +44,2 @@ "use strict"; | ||
var axios_1 = __importDefault(require("axios")); | ||
// @ts-ignore | ||
var constant_1 = require("@compassdigital/constant"); | ||
@@ -47,0 +46,0 @@ function api(token) { |
@@ -0,1 +1,2 @@ | ||
import { User, GetUserAuthResponse, DeleteUserResponse } from '@compassdigital/sdk.typescript/interface/user'; | ||
export interface UserModule { | ||
@@ -7,7 +8,5 @@ get_token({ type, id, email, password, }: { | ||
password?: string; | ||
}): Promise<string | Error>; | ||
create_user(realm?: string): Promise<User | Error>; | ||
delete_user(token: string, id: string): Promise<{ | ||
success: boolean; | ||
} | Error>; | ||
}): Promise<GetUserAuthResponse['token']>; | ||
create_user(realm?: string): Promise<User>; | ||
delete_user(token: string, id: string): Promise<DeleteUserResponse>; | ||
} | ||
@@ -17,18 +16,1 @@ export declare enum TokenType { | ||
} | ||
export interface CreateUserReqPayload { | ||
email: string; | ||
name: { | ||
first: string; | ||
last: string; | ||
}; | ||
password: string; | ||
phone: number; | ||
birthday: Date; | ||
realm?: string; | ||
} | ||
export interface User extends CreateUserReqPayload { | ||
id: string; | ||
} | ||
export declare type DeleteUserRes = { | ||
success: boolean; | ||
} | Error; |
@@ -66,22 +66,9 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var token, err_1, token, err_2; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!(type === interface_1.TokenType.ADMIN && id)) return [3 /*break*/, 4]; | ||
_b.label = 1; | ||
case 1: | ||
_b.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, auth.create_access_token(id, 60 * 10000, '*:*:*')]; | ||
case 2: | ||
token = _b.sent(); | ||
return [2 /*return*/, token]; | ||
case 3: | ||
err_1 = _b.sent(); | ||
throw new Error(err_1); | ||
case 4: | ||
if (!(email && password)) return [3 /*break*/, 8]; | ||
_b.label = 5; | ||
case 5: | ||
_b.trys.push([5, 7, , 8]); | ||
if (type === interface_1.TokenType.ADMIN && id) { | ||
return [2 /*return*/, auth.create_access_token(id, 60 * 10000, '*:*:*')]; | ||
} | ||
if (!(email && password)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, axios_1.api().get('/user/auth', { | ||
@@ -93,9 +80,4 @@ auth: { | ||
})]; | ||
case 6: | ||
token = (_b.sent()).data.token; | ||
return [2 /*return*/, token]; | ||
case 7: | ||
err_2 = _b.sent(); | ||
throw new Error(err_2); | ||
case 8: throw new Error('need more parameters to get token'); | ||
case 1: return [2 /*return*/, (_b.sent()).data.token]; | ||
case 2: throw new Error('need more parameters to get token'); | ||
} | ||
@@ -107,3 +89,3 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var user_payload, user, err_3; | ||
var user_payload, user; | ||
return __generator(this, function (_a) { | ||
@@ -120,3 +102,3 @@ switch (_a.label) { | ||
phone: 1234567890, | ||
birthday: new Date(), | ||
birthday: new Date().toJSON(), | ||
}; | ||
@@ -126,13 +108,6 @@ if (realm) { | ||
} | ||
_a.label = 1; | ||
return [4 /*yield*/, axios_1.api().post("/user", user_payload)]; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, axios_1.api().post("/user", user_payload)]; | ||
case 2: | ||
user = (_a.sent()).data; | ||
return [2 /*return*/, __assign(__assign({}, user_payload), { id: user.id })]; | ||
case 3: | ||
err_3 = _a.sent(); | ||
throw new Error(err_3); | ||
case 4: return [2 /*return*/]; | ||
} | ||
@@ -144,15 +119,6 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data, err_4; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, axios_1.api(token).delete("/user/" + id)]; | ||
case 1: | ||
data = (_a.sent()).data; | ||
return [2 /*return*/, data]; | ||
case 2: | ||
err_4 = _a.sent(); | ||
throw new Error(err_4); | ||
case 3: return [2 /*return*/]; | ||
case 0: return [4 /*yield*/, axios_1.api(token).delete("/user/" + id)]; | ||
case 1: return [2 /*return*/, (_a.sent()).data]; | ||
} | ||
@@ -159,0 +125,0 @@ }); |
{ | ||
"name": "@compassdigital/helper", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -29,3 +29,4 @@ "main": "lib/index.js", | ||
"@compassdigital/auth": "^4.0.1", | ||
"@compassdigital/constant": "^0.3.1", | ||
"@compassdigital/constant": "^0.4.0", | ||
"@compassdigital/sdk.typescript": "^3.0.0-beta.20", | ||
"axios": "^0.21.1", | ||
@@ -32,0 +33,0 @@ "jwt-decode": "^3.1.2", |
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
// @ts-ignore | ||
import { CDL_API_URL } from '@compassdigital/constant'; | ||
@@ -4,0 +3,0 @@ |
@@ -0,1 +1,7 @@ | ||
import { | ||
User, | ||
GetUserAuthResponse, | ||
DeleteUserResponse, | ||
} from '@compassdigital/sdk.typescript/interface/user'; | ||
export interface UserModule { | ||
@@ -12,5 +18,5 @@ get_token({ | ||
password?: string; | ||
}): Promise<string | Error>; | ||
create_user(realm?: string): Promise<User | Error>; | ||
delete_user(token: string, id: string): Promise<{ success: boolean } | Error>; | ||
}): Promise<GetUserAuthResponse['token']>; | ||
create_user(realm?: string): Promise<User>; | ||
delete_user(token: string, id: string): Promise<DeleteUserResponse>; | ||
} | ||
@@ -21,19 +27,1 @@ | ||
} | ||
export interface CreateUserReqPayload { | ||
email: string; | ||
name: { | ||
first: string; | ||
last: string; | ||
}; | ||
password: string; | ||
phone: number; | ||
birthday: Date; | ||
realm?: string; | ||
} | ||
export interface User extends CreateUserReqPayload { | ||
id: string; | ||
} | ||
export type DeleteUserRes = { success: boolean } | Error; |
import Auth from '@compassdigital/auth'; | ||
import { | ||
User, | ||
GetUserAuthResponse, | ||
PostUserBody, | ||
DeleteUserResponse, | ||
} from '@compassdigital/sdk.typescript/interface/user'; | ||
import { api } from '../../axios'; | ||
import { UserModule, TokenType, CreateUserReqPayload, User, DeleteUserRes } from './interface'; | ||
import { UserModule, TokenType } from './interface'; | ||
@@ -24,16 +30,9 @@ const auth = new Auth({ | ||
password?: string; | ||
}): Promise<string | Error> { | ||
}): Promise<GetUserAuthResponse['token']> { | ||
if (type === TokenType.ADMIN && id) { | ||
try { | ||
const token: string = await auth.create_access_token(id, 60 * 10000, '*:*:*'); | ||
return token; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
return auth.create_access_token(id, 60 * 10000, '*:*:*'); | ||
} | ||
if (email && password) { | ||
try { | ||
const { | ||
data: { token }, | ||
}: { data: { token: string }; token: string } = await api().get('/user/auth', { | ||
return ( | ||
await api().get<GetUserAuthResponse>('/user/auth', { | ||
auth: { | ||
@@ -43,12 +42,9 @@ username: email, | ||
}, | ||
}); | ||
return token; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
}) | ||
).data.token; | ||
} | ||
throw new Error('need more parameters to get token'); | ||
} | ||
async function create_user(realm?: string): Promise<User | Error> { | ||
const user_payload: CreateUserReqPayload = { | ||
async function create_user(realm?: string): Promise<User> { | ||
const user_payload: PostUserBody = { | ||
email: `unit-test${Date.now()}@mailsac.com`, | ||
@@ -61,3 +57,3 @@ name: { | ||
phone: 1234567890, | ||
birthday: new Date(), | ||
birthday: new Date().toJSON(), | ||
}; | ||
@@ -67,17 +63,8 @@ if (realm) { | ||
} | ||
try { | ||
const { data: user }: { data: { id: string } } = await api().post(`/user`, user_payload); | ||
return { ...user_payload, id: user.id }; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
const { data: user } = await api().post<User>(`/user`, user_payload); | ||
return { ...user_payload, id: user.id }; | ||
} | ||
async function delete_user(token: string, id: string): Promise<DeleteUserRes> { | ||
try { | ||
const { data }: { data: { success: boolean } } = await api(token).delete(`/user/${id}`); | ||
return data; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
async function delete_user(token: string, id: string): Promise<DeleteUserResponse> { | ||
return (await api(token).delete<DeleteUserResponse>(`/user/${id}`)).data; | ||
} | ||
export = methods; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26273
6
528
+ Added@compassdigital/constant@0.4.2(transitive)
- Removed@compassdigital/constant@0.3.1(transitive)