skapi-js 0.2.0 → 0.2.2
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/skapi.js +1 -1
- package/dist/skapi.js.map +1 -1
- package/dist/skapi.module.js +1 -1
- package/dist/skapi.module.js.map +1 -1
- package/js/Main.d.ts +3 -0
- package/js/Main.js +3 -0
- package/js/Types.d.ts +194 -0
- package/js/Types.js +1 -0
- package/js/main/error.d.ts +8 -0
- package/js/main/error.js +38 -0
- package/js/main/skapi.d.ts +79 -0
- package/js/main/skapi.js +285 -0
- package/js/methods/database.d.ts +83 -0
- package/js/methods/database.js +915 -0
- package/js/methods/request.d.ts +36 -0
- package/js/methods/request.js +656 -0
- package/js/methods/subscription.d.ts +46 -0
- package/js/methods/subscription.js +240 -0
- package/js/methods/user.d.ts +67 -0
- package/js/methods/user.js +747 -0
- package/js/utils/utils.d.ts +20 -0
- package/js/utils/utils.js +286 -0
- package/js/utils/validator.d.ts +19 -0
- package/js/utils/validator.js +294 -0
- package/package.json +5 -3
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DatabaseResponse, FetchOptions, FormSubmitCallback, Form, Newsletters, SubscriptionGroup } from '../Types';
|
|
2
|
+
export declare function getSubscriptions(params: {
|
|
3
|
+
subscriber?: string;
|
|
4
|
+
subscription?: string;
|
|
5
|
+
group?: number;
|
|
6
|
+
blocked?: boolean;
|
|
7
|
+
}, fetchOptions?: FetchOptions, _mapper?: Function): Promise<DatabaseResponse<{
|
|
8
|
+
subscriber: string;
|
|
9
|
+
subscription: string;
|
|
10
|
+
group: number;
|
|
11
|
+
timestamp: number;
|
|
12
|
+
blocked: boolean;
|
|
13
|
+
}>>;
|
|
14
|
+
export declare function subscribe(option: SubscriptionGroup<number>): Promise<'SUCCESS: the user has subscribed.'>;
|
|
15
|
+
export declare function unsubscribe(option: SubscriptionGroup<number | '*'>): Promise<'SUCCESS: the user has unsubscribed.'>;
|
|
16
|
+
export declare function blockSubscriber(option: SubscriptionGroup<number | '*'>): Promise<'SUCCESS: blocked user id "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'>;
|
|
17
|
+
export declare function unblockSubscriber(option: SubscriptionGroup<number | '*'>): Promise<'SUCCESS: unblocked user id "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'>;
|
|
18
|
+
export declare function getSubscribedTo(option: SubscriptionGroup<number | undefined> & {
|
|
19
|
+
blocked?: boolean;
|
|
20
|
+
}, fetchOptions: FetchOptions): Promise<DatabaseResponse<any>>;
|
|
21
|
+
export declare function getSubscribers(option: SubscriptionGroup<number | undefined> & {
|
|
22
|
+
blocked?: boolean;
|
|
23
|
+
}, fetchOptions: FetchOptions): Promise<DatabaseResponse<any>>;
|
|
24
|
+
export declare function getNewsletterSubscription(params: {
|
|
25
|
+
group?: number;
|
|
26
|
+
}): Promise<{
|
|
27
|
+
active: boolean;
|
|
28
|
+
timestamp: number;
|
|
29
|
+
group: number;
|
|
30
|
+
subscribed_email: string;
|
|
31
|
+
}[]>;
|
|
32
|
+
export declare function subscribeNewsletter(form: Form<{
|
|
33
|
+
email?: string;
|
|
34
|
+
group: number | 'public' | 'authorized';
|
|
35
|
+
redirect?: string;
|
|
36
|
+
}>, callbacks: FormSubmitCallback): Promise<string>;
|
|
37
|
+
export declare function unsubscribeNewsletter(params: {
|
|
38
|
+
group: number | 'public' | 'authorized' | null;
|
|
39
|
+
}): Promise<string>;
|
|
40
|
+
export declare function getNewsletters(params?: {
|
|
41
|
+
searchFor: 'message_id' | 'timestamp' | 'read' | 'complaint' | 'subject';
|
|
42
|
+
value: string | number;
|
|
43
|
+
range: string | number;
|
|
44
|
+
condition?: '>' | '>=' | '=' | '<' | '<=' | 'gt' | 'gte' | 'eq' | 'lt' | 'lte';
|
|
45
|
+
group: 'public' | 'authorized' | number;
|
|
46
|
+
}, fetchOptions?: FetchOptions): Promise<Newsletters>;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import SkapiError from '../main/error';
|
|
2
|
+
import validator from '../utils/validator';
|
|
3
|
+
import { request } from './request';
|
|
4
|
+
function subscriptionGroupCheck(option) {
|
|
5
|
+
option = validator.Params(option, {
|
|
6
|
+
user_id: (v) => validator.UserId(v, '"user_id"'),
|
|
7
|
+
group: (v) => {
|
|
8
|
+
if (v === '*') {
|
|
9
|
+
return v;
|
|
10
|
+
}
|
|
11
|
+
if (typeof v !== 'number') {
|
|
12
|
+
throw new SkapiError('"group" should be type: number.', { code: 'INVALID_PARAMETER' });
|
|
13
|
+
}
|
|
14
|
+
else if (v < 1 && v > 99) {
|
|
15
|
+
throw new SkapiError('"group" should be within range 1 ~ 99.', { code: 'INVALID_PARAMETER' });
|
|
16
|
+
}
|
|
17
|
+
return v;
|
|
18
|
+
}
|
|
19
|
+
}, ['user_id', 'group']);
|
|
20
|
+
if (this.__user && option.user_id === this.__user.user_id) {
|
|
21
|
+
throw new SkapiError(`"user_id" cannot be the user's own ID.`, { code: 'INVALID_PARAMETER' });
|
|
22
|
+
}
|
|
23
|
+
return option;
|
|
24
|
+
}
|
|
25
|
+
;
|
|
26
|
+
export async function getSubscriptions(params, fetchOptions, _mapper) {
|
|
27
|
+
params = validator.Params(params, {
|
|
28
|
+
subscriber: (v) => validator.UserId(v, 'User ID in "subscriber"'),
|
|
29
|
+
group: 'number',
|
|
30
|
+
subscription: (v) => validator.UserId(v, 'User ID in "subscription"'),
|
|
31
|
+
blocked: 'boolean'
|
|
32
|
+
});
|
|
33
|
+
if (!params.subscriber && !params.subscription) {
|
|
34
|
+
throw new SkapiError('At least either "subscriber" or "subscription" should have a value.', { code: 'INVALID_PARAMETER' });
|
|
35
|
+
}
|
|
36
|
+
let response = await request.bind(this)('get-subscription', params, Object.assign({ auth: true }, { fetchOptions }));
|
|
37
|
+
response.list = response.list.map(_mapper || ((s) => {
|
|
38
|
+
let subscription = {};
|
|
39
|
+
let subSplit = s.sub.split('#');
|
|
40
|
+
subscription.subscriber = subSplit[2];
|
|
41
|
+
subscription.subscription = subSplit[0];
|
|
42
|
+
subscription.group = parseInt(subSplit[1]);
|
|
43
|
+
subscription.timestamp = s.stmp;
|
|
44
|
+
subscription.blocked = s.grp.substring(0, 1) === 'N';
|
|
45
|
+
return subscription;
|
|
46
|
+
}));
|
|
47
|
+
return response;
|
|
48
|
+
}
|
|
49
|
+
export async function subscribe(option) {
|
|
50
|
+
await this.__connection;
|
|
51
|
+
let { user_id, group } = subscriptionGroupCheck.bind(this)(option);
|
|
52
|
+
if (typeof group !== 'number') {
|
|
53
|
+
throw new SkapiError('"group" should be type: number.', { code: 'INVALID_PARAMETER' });
|
|
54
|
+
}
|
|
55
|
+
return await request.bind(this)('subscription', {
|
|
56
|
+
subscribe: user_id,
|
|
57
|
+
group
|
|
58
|
+
}, { auth: true });
|
|
59
|
+
}
|
|
60
|
+
export async function unsubscribe(option) {
|
|
61
|
+
await this.__connection;
|
|
62
|
+
let { user_id, group } = subscriptionGroupCheck.bind(this)(option);
|
|
63
|
+
return await request.bind(this)('subscription', {
|
|
64
|
+
unsubscribe: user_id,
|
|
65
|
+
group
|
|
66
|
+
}, { auth: true });
|
|
67
|
+
}
|
|
68
|
+
export async function blockSubscriber(option) {
|
|
69
|
+
await this.__connection;
|
|
70
|
+
let { user_id, group } = subscriptionGroupCheck.bind(this)(option);
|
|
71
|
+
return await request.bind(this)('subscription', { block: user_id, group }, { auth: true });
|
|
72
|
+
}
|
|
73
|
+
export async function unblockSubscriber(option) {
|
|
74
|
+
await this.__connection;
|
|
75
|
+
let { user_id, group } = subscriptionGroupCheck.bind(this)(option);
|
|
76
|
+
return await request.bind(this)('subscription', { unblock: user_id, group }, { auth: true });
|
|
77
|
+
}
|
|
78
|
+
export async function getSubscribedTo(option, fetchOptions) {
|
|
79
|
+
await this.__connection;
|
|
80
|
+
option = validator.Params(option, {
|
|
81
|
+
user_id: (v) => validator.UserId(v, '"user_id"'),
|
|
82
|
+
group: 'number',
|
|
83
|
+
blocked: 'boolean'
|
|
84
|
+
}) || {};
|
|
85
|
+
return getSubscriptions.bind(this)({
|
|
86
|
+
subscriber: option.user_id || this.__user?.user_id,
|
|
87
|
+
group: option.group,
|
|
88
|
+
blocked: option.blocked
|
|
89
|
+
}, fetchOptions);
|
|
90
|
+
}
|
|
91
|
+
;
|
|
92
|
+
export async function getSubscribers(option, fetchOptions) {
|
|
93
|
+
await this.__connection;
|
|
94
|
+
option = validator.Params(option, {
|
|
95
|
+
user_id: (v) => validator.UserId(v, '"user_id"'),
|
|
96
|
+
group: 'number',
|
|
97
|
+
blocked: 'boolean'
|
|
98
|
+
}) || {};
|
|
99
|
+
let subParams = {
|
|
100
|
+
subscription: option.user_id || this.__user?.user_id,
|
|
101
|
+
group: option.group,
|
|
102
|
+
blocked: option.blocked
|
|
103
|
+
};
|
|
104
|
+
return getSubscriptions.bind(this)(subParams, fetchOptions);
|
|
105
|
+
}
|
|
106
|
+
;
|
|
107
|
+
export async function getNewsletterSubscription(params) {
|
|
108
|
+
await this.__connection;
|
|
109
|
+
let isAdmin = await this.checkAdmin();
|
|
110
|
+
params = validator.Params(params, {
|
|
111
|
+
user_id: v => {
|
|
112
|
+
if (v !== this.__user.user_id && !isAdmin) {
|
|
113
|
+
throw new SkapiError(`No access.`, { code: 'INVALID_REQUEST' });
|
|
114
|
+
}
|
|
115
|
+
return v;
|
|
116
|
+
},
|
|
117
|
+
group: 'number'
|
|
118
|
+
});
|
|
119
|
+
let list = await request.bind(this)('get-newsletter-subscription', params, { auth: true });
|
|
120
|
+
let result = [];
|
|
121
|
+
for (let sub of list) {
|
|
122
|
+
let subt = sub['subt'].split('#');
|
|
123
|
+
let active = true;
|
|
124
|
+
if (subt[0].charAt(0) === '@') {
|
|
125
|
+
active = false;
|
|
126
|
+
subt[0] = subt[0].substring(1);
|
|
127
|
+
}
|
|
128
|
+
let group = parseInt(subt[0]);
|
|
129
|
+
result.push({
|
|
130
|
+
timestamp: sub['stmp'],
|
|
131
|
+
group,
|
|
132
|
+
subscribed_email: subt[1],
|
|
133
|
+
active
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
export async function subscribeNewsletter(form, callbacks) {
|
|
139
|
+
await this.__connection;
|
|
140
|
+
let params = validator.Params(form || {}, {
|
|
141
|
+
email: (v) => validator.Email(v),
|
|
142
|
+
group: ['number', 'public', 'authorized'],
|
|
143
|
+
redirect: (v) => validator.Url(v)
|
|
144
|
+
}, this.__user ? ['group'] : ['email', 'group']);
|
|
145
|
+
return request.bind(this)(`subscribe-${this.__user ? '' : 'public-'}newsletter`, params, { fetchOptions: callbacks, auth: !!this.__user });
|
|
146
|
+
}
|
|
147
|
+
export async function unsubscribeNewsletter(params) {
|
|
148
|
+
await this.__connection;
|
|
149
|
+
params = validator.Params(params, {
|
|
150
|
+
group: ['number', 'public', 'authorized']
|
|
151
|
+
}, ['group']);
|
|
152
|
+
let param_send = Object.assign({
|
|
153
|
+
action: 'unsubscribe'
|
|
154
|
+
}, params);
|
|
155
|
+
return request.bind(this)('subscribe-newsletter', param_send, { auth: true });
|
|
156
|
+
}
|
|
157
|
+
export async function getNewsletters(params, fetchOptions) {
|
|
158
|
+
let isAdmin = await this.checkAdmin();
|
|
159
|
+
let searchType = {
|
|
160
|
+
'message_id': 'string',
|
|
161
|
+
'timestamp': 'number',
|
|
162
|
+
'read': 'number',
|
|
163
|
+
'complaint': 'number',
|
|
164
|
+
'subject': 'string'
|
|
165
|
+
};
|
|
166
|
+
if (!params) {
|
|
167
|
+
if (!fetchOptions) {
|
|
168
|
+
fetchOptions = {};
|
|
169
|
+
}
|
|
170
|
+
fetchOptions.ascending = false;
|
|
171
|
+
}
|
|
172
|
+
let _params = params || {
|
|
173
|
+
searchFor: 'timestamp',
|
|
174
|
+
value: 0,
|
|
175
|
+
condition: '>'
|
|
176
|
+
};
|
|
177
|
+
params = validator.Params(_params, {
|
|
178
|
+
searchFor: ['message_id', 'timestamp', 'read', 'complaint', 'group', 'subject'],
|
|
179
|
+
value: (v) => {
|
|
180
|
+
if (typeof v !== searchType[_params.searchFor]) {
|
|
181
|
+
throw new SkapiError(`"value" type does not match the type of "${_params.searchFor}" index.`, { code: 'INVALID_PARAMETER' });
|
|
182
|
+
}
|
|
183
|
+
else if (typeof v === 'string' && !v) {
|
|
184
|
+
throw new SkapiError('"value" should not be empty string.', { code: 'INVALID_PARAMETER' });
|
|
185
|
+
}
|
|
186
|
+
return v;
|
|
187
|
+
},
|
|
188
|
+
range: (v) => {
|
|
189
|
+
if (!_params.hasOwnProperty('value') || typeof v !== typeof _params.value) {
|
|
190
|
+
throw new SkapiError('"range" should match type of "value".', { code: 'INVALID_PARAMETER' });
|
|
191
|
+
}
|
|
192
|
+
return v;
|
|
193
|
+
},
|
|
194
|
+
condition: ['>', '>=', '=', '<', '<=', 'gt', 'gte', 'eq', 'lt', 'lte', () => '='],
|
|
195
|
+
group: (x) => {
|
|
196
|
+
if (!this.session) {
|
|
197
|
+
throw new SkapiError('User should be logged in.', { code: 'INVALID_REQUEST' });
|
|
198
|
+
}
|
|
199
|
+
if (x === 'public') {
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
else if (x === 'authorized') {
|
|
203
|
+
return 1;
|
|
204
|
+
}
|
|
205
|
+
else if (typeof x === 'number') {
|
|
206
|
+
if (!isAdmin && x > parseInt(this.session.idToken.payload.access_group)) {
|
|
207
|
+
throw new SkapiError('User has no access.', { code: 'INVALID_REQUEST' });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return x;
|
|
211
|
+
}
|
|
212
|
+
}, ['searchFor', 'value', 'group']);
|
|
213
|
+
let mails = await request.bind(this)(params.group === 0 ? 'get-public-newsletters' : 'get-newsletters', params, Object.assign({ method: 'get', auth: params.group !== 0 }, { fetchOptions }));
|
|
214
|
+
let remap = {
|
|
215
|
+
'message_id': 'mid',
|
|
216
|
+
'timestamp': 'stmp',
|
|
217
|
+
'complaint': 'cmpl',
|
|
218
|
+
'read': 'read',
|
|
219
|
+
'subject': 'subj',
|
|
220
|
+
'bounced': 'bnce',
|
|
221
|
+
'url': 'url'
|
|
222
|
+
};
|
|
223
|
+
let defaults = {
|
|
224
|
+
'message_id': '',
|
|
225
|
+
'timestamp': 0,
|
|
226
|
+
'complaint': 0,
|
|
227
|
+
'read': 0,
|
|
228
|
+
'subject': '',
|
|
229
|
+
'bounced': 0,
|
|
230
|
+
'url': ''
|
|
231
|
+
};
|
|
232
|
+
mails.list = mails.list.map(m => {
|
|
233
|
+
let remapped = {};
|
|
234
|
+
for (let k in remap) {
|
|
235
|
+
remapped[k] = m[remap[k]] || defaults[remap[k]];
|
|
236
|
+
}
|
|
237
|
+
return remapped;
|
|
238
|
+
});
|
|
239
|
+
return mails;
|
|
240
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { CognitoUser, CognitoUserSession, CognitoUserPool } from 'amazon-cognito-identity-js';
|
|
2
|
+
import { User, Form, FormSubmitCallback, UserProfile, FetchOptions, DatabaseResponse, QueryParams, UserAttributes } from '../Types';
|
|
3
|
+
export declare let userPool: CognitoUserPool | null;
|
|
4
|
+
export declare function setUserPool(params: {
|
|
5
|
+
UserPoolId: string;
|
|
6
|
+
ClientId: string;
|
|
7
|
+
}): void;
|
|
8
|
+
export declare function authentication(): {
|
|
9
|
+
getSession: (option?: {
|
|
10
|
+
refreshToken?: boolean;
|
|
11
|
+
}) => Promise<CognitoUserSession>;
|
|
12
|
+
authenticateUser: (email: string, password: string) => Promise<User>;
|
|
13
|
+
createCognitoUser: (email: string) => Promise<{
|
|
14
|
+
cognitoUser: CognitoUser;
|
|
15
|
+
cognitoUsername: string;
|
|
16
|
+
}>;
|
|
17
|
+
getUser: () => Promise<UserProfile | null>;
|
|
18
|
+
};
|
|
19
|
+
export declare function getProfile(options?: {
|
|
20
|
+
refreshToken: boolean;
|
|
21
|
+
}): Promise<User | null>;
|
|
22
|
+
export declare function checkAdmin(): Promise<boolean>;
|
|
23
|
+
export declare function logout(e: SubmitEvent): Promise<'SUCCESS: The user has been logged out.'>;
|
|
24
|
+
export declare function resendSignupConfirmation(redirect: string): Promise<'SUCCESS: Signup confirmation E-Mail has been sent.'>;
|
|
25
|
+
export declare function recoverAccount(redirect?: boolean | string): Promise<"SUCCESS: Recovery e-mail has been sent.">;
|
|
26
|
+
export declare function login(form: Form<{
|
|
27
|
+
email: string;
|
|
28
|
+
password: string;
|
|
29
|
+
}>, option?: FormSubmitCallback & {
|
|
30
|
+
logout: boolean;
|
|
31
|
+
}): Promise<User>;
|
|
32
|
+
export declare function signup(form: Form<UserAttributes & {
|
|
33
|
+
email: String;
|
|
34
|
+
password: String;
|
|
35
|
+
}>, option?: {
|
|
36
|
+
signup_confirmation?: boolean | string;
|
|
37
|
+
email_subscription?: boolean;
|
|
38
|
+
login?: boolean;
|
|
39
|
+
} & FormSubmitCallback): Promise<User | "SUCCESS: The account has been created. User's signup confirmation is required." | 'SUCCESS: The account has been created.'>;
|
|
40
|
+
export declare function disableAccount(): Promise<'SUCCESS: account has been disabled.'>;
|
|
41
|
+
export declare function resetPassword(form: Form<{
|
|
42
|
+
email: string;
|
|
43
|
+
code: string | number;
|
|
44
|
+
new_password: string;
|
|
45
|
+
}>, option?: FormSubmitCallback): Promise<"SUCCESS: New password has been set.">;
|
|
46
|
+
export declare function verifyPhoneNumber(form: Form<{
|
|
47
|
+
code: string;
|
|
48
|
+
}>): Promise<'SUCCESS: Verification code has been sent.' | 'SUCCESS: "phone_number" is verified.'>;
|
|
49
|
+
export declare function verifyEmail(form: Form<{
|
|
50
|
+
code: string;
|
|
51
|
+
}>): Promise<'SUCCESS: Verification code has been sent.' | 'SUCCESS: "email" is verified.'>;
|
|
52
|
+
export declare function forgotPassword(form: Form<{
|
|
53
|
+
email: string;
|
|
54
|
+
}>, option?: FormSubmitCallback): Promise<"SUCCESS: Verification code has been sent.">;
|
|
55
|
+
export declare function changePassword(params: {
|
|
56
|
+
new_password: string;
|
|
57
|
+
current_password: string;
|
|
58
|
+
}): Promise<unknown>;
|
|
59
|
+
export declare function updateProfile(form: Form<UserAttributes>, option?: FormSubmitCallback): Promise<any>;
|
|
60
|
+
export declare function getUsers(params?: QueryParams | null, fetchOptions?: FetchOptions): Promise<DatabaseResponse<UserAttributes>>;
|
|
61
|
+
export declare function lastVerifiedEmail(params?: {
|
|
62
|
+
revert: boolean;
|
|
63
|
+
}): Promise<string | UserProfile>;
|
|
64
|
+
export declare function requestUsernameChange(params: {
|
|
65
|
+
redirect?: string;
|
|
66
|
+
username: string;
|
|
67
|
+
}): Promise<'SUCCESS: ...'>;
|