@tencentcloud/web-push 1.0.3 → 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/CHANGELOG.md +63 -0
- package/README.md +100 -92
- package/{dist/index.esm.js → index.esm.js} +1 -1
- package/{dist/index.umd.js → index.umd.js} +1 -1
- package/package.json +9 -47
- package/src/__tests__/index.test.ts +0 -120
- package/src/__tests__/integration.test.ts +0 -285
- package/src/__tests__/setup.ts +0 -210
- package/src/__tests__/types.test.ts +0 -303
- package/src/__tests__/web-push-sdk.test.ts +0 -257
- package/src/components/message-popup.ts +0 -1007
- package/src/core/event-emitter.ts +0 -61
- package/src/core/service-worker-manager.ts +0 -614
- package/src/core/web-push-sdk.ts +0 -690
- package/src/debug/GenerateTestUserSig.js +0 -37
- package/src/debug/index.d.ts +0 -6
- package/src/debug/index.js +0 -1
- package/src/debug/lib-generate-test-usersig-es.min.js +0 -2
- package/src/index.ts +0 -9
- package/src/service-worker/sw.ts +0 -494
- package/src/types/index.ts +0 -2
- package/src/types/inner.ts +0 -44
- package/src/types/outer.ts +0 -142
- package/src/utils/browser-support.ts +0 -412
- package/src/utils/logger.ts +0 -66
- package/src/utils/storage.ts +0 -51
- package/src/utils/validator.ts +0 -267
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/src → src}/components/message-popup.d.ts +0 -0
- /package/{dist/src → src}/core/event-emitter.d.ts +0 -0
- /package/{dist/src → src}/core/service-worker-manager.d.ts +0 -0
- /package/{dist/src → src}/core/web-push-sdk.d.ts +0 -0
- /package/{dist/src → src}/index.d.ts +0 -0
- /package/{dist/src → src}/service-worker/sw.d.ts +0 -0
- /package/{dist/src → src}/types/index.d.ts +0 -0
- /package/{dist/src → src}/types/inner.d.ts +0 -0
- /package/{dist/src → src}/types/outer.d.ts +0 -0
- /package/{dist/src → src}/utils/browser-support.d.ts +0 -0
- /package/{dist/src → src}/utils/logger.d.ts +0 -0
- /package/{dist/src → src}/utils/storage.d.ts +0 -0
- /package/{dist/src → src}/utils/validator.d.ts +0 -0
- /package/{dist/sw.js → sw.js} +0 -0
package/src/utils/validator.ts
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parameter validation utility class
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export interface ValidationRule {
|
|
6
|
-
required?: boolean;
|
|
7
|
-
type?: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'function';
|
|
8
|
-
min?: number;
|
|
9
|
-
max?: number;
|
|
10
|
-
pattern?: RegExp;
|
|
11
|
-
custom?: (value: any) => boolean | string;
|
|
12
|
-
message?: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface ValidationSchema {
|
|
16
|
-
[key: string]: ValidationRule;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class ValidationError extends Error {
|
|
20
|
-
public field: string;
|
|
21
|
-
public value: any;
|
|
22
|
-
|
|
23
|
-
constructor(field: string, message: string, value?: any) {
|
|
24
|
-
super(`Parameter validation failed: ${field} - ${message}`);
|
|
25
|
-
this.name = 'ValidationError';
|
|
26
|
-
this.field = field;
|
|
27
|
-
this.value = value;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class Validator {
|
|
32
|
-
/**
|
|
33
|
-
* Validate single value
|
|
34
|
-
*/
|
|
35
|
-
static validateValue(
|
|
36
|
-
fieldName: string,
|
|
37
|
-
value: any,
|
|
38
|
-
rule: ValidationRule
|
|
39
|
-
): void {
|
|
40
|
-
if (
|
|
41
|
-
rule.required &&
|
|
42
|
-
(value === undefined || value === null || value === '')
|
|
43
|
-
) {
|
|
44
|
-
throw new ValidationError(
|
|
45
|
-
fieldName,
|
|
46
|
-
rule.message || `${fieldName} is required`,
|
|
47
|
-
value
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
!rule.required &&
|
|
53
|
-
(value === undefined || value === null || value === '')
|
|
54
|
-
) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (rule.type && !this.checkType(value, rule.type)) {
|
|
59
|
-
throw new ValidationError(
|
|
60
|
-
fieldName,
|
|
61
|
-
rule.message || `${fieldName} must be of type ${rule.type}`,
|
|
62
|
-
value
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (rule.type === 'string' && typeof value === 'string') {
|
|
67
|
-
if (rule.min !== undefined && value.length < rule.min) {
|
|
68
|
-
throw new ValidationError(
|
|
69
|
-
fieldName,
|
|
70
|
-
rule.message ||
|
|
71
|
-
`${fieldName} length cannot be less than ${rule.min} characters`,
|
|
72
|
-
value
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
if (rule.max !== undefined && value.length > rule.max) {
|
|
76
|
-
throw new ValidationError(
|
|
77
|
-
fieldName,
|
|
78
|
-
rule.message ||
|
|
79
|
-
`${fieldName} length cannot exceed ${rule.max} characters`,
|
|
80
|
-
value
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (rule.type === 'number' && typeof value === 'number') {
|
|
86
|
-
if (rule.min !== undefined && value < rule.min) {
|
|
87
|
-
throw new ValidationError(
|
|
88
|
-
fieldName,
|
|
89
|
-
rule.message || `${fieldName} cannot be less than ${rule.min}`,
|
|
90
|
-
value
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
if (rule.max !== undefined && value > rule.max) {
|
|
94
|
-
throw new ValidationError(
|
|
95
|
-
fieldName,
|
|
96
|
-
rule.message || `${fieldName} cannot be greater than ${rule.max}`,
|
|
97
|
-
value
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (rule.pattern && typeof value === 'string') {
|
|
103
|
-
if (!rule.pattern.test(value)) {
|
|
104
|
-
throw new ValidationError(
|
|
105
|
-
fieldName,
|
|
106
|
-
rule.message || `${fieldName} format is incorrect`,
|
|
107
|
-
value
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (rule.custom) {
|
|
113
|
-
const result = rule.custom(value);
|
|
114
|
-
if (result !== true) {
|
|
115
|
-
const message =
|
|
116
|
-
typeof result === 'string'
|
|
117
|
-
? result
|
|
118
|
-
: rule.message || `${fieldName} validation failed`;
|
|
119
|
-
throw new ValidationError(fieldName, message, value);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Validate object
|
|
126
|
-
*/
|
|
127
|
-
static validateObject(obj: any, schema: ValidationSchema): void {
|
|
128
|
-
if (!obj || typeof obj !== 'object') {
|
|
129
|
-
throw new ValidationError('root', 'Parameter must be an object type');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
for (const [fieldName, rule] of Object.entries(schema)) {
|
|
133
|
-
const value = obj[fieldName];
|
|
134
|
-
this.validateValue(fieldName, value, rule);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Check type
|
|
140
|
-
*/
|
|
141
|
-
private static checkType(value: any, expectedType: string): boolean {
|
|
142
|
-
switch (expectedType) {
|
|
143
|
-
case 'string':
|
|
144
|
-
return typeof value === 'string';
|
|
145
|
-
case 'number':
|
|
146
|
-
return typeof value === 'number' && !isNaN(value);
|
|
147
|
-
case 'boolean':
|
|
148
|
-
return typeof value === 'boolean';
|
|
149
|
-
case 'object':
|
|
150
|
-
return (
|
|
151
|
-
value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
152
|
-
);
|
|
153
|
-
case 'array':
|
|
154
|
-
return Array.isArray(value);
|
|
155
|
-
case 'function':
|
|
156
|
-
return typeof value === 'function';
|
|
157
|
-
default:
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
static validateSDKAppID(SDKAppID: any): void {
|
|
163
|
-
this.validateValue('SDKAppID', SDKAppID, {
|
|
164
|
-
required: true,
|
|
165
|
-
type: 'number',
|
|
166
|
-
custom: (value) => {
|
|
167
|
-
if (!Number.isInteger(value)) {
|
|
168
|
-
return 'SDKAppID must be an integer';
|
|
169
|
-
}
|
|
170
|
-
return true;
|
|
171
|
-
},
|
|
172
|
-
message: 'SDKAppID must be an integer between 1-999999999',
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
static validateAppKey(appKey: any): void {
|
|
177
|
-
this.validateValue('appKey', appKey, {
|
|
178
|
-
required: true,
|
|
179
|
-
type: 'string',
|
|
180
|
-
pattern: /^[a-zA-Z0-9_-]+$/,
|
|
181
|
-
message:
|
|
182
|
-
'appKey must be a 1-256 character string composed of letters, numbers, underscores or hyphens',
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
static validateUserID(userID: any): void {
|
|
187
|
-
this.validateValue('userID', userID, {
|
|
188
|
-
required: true,
|
|
189
|
-
type: 'string',
|
|
190
|
-
pattern: /^[a-zA-Z0-9_.-]+$/,
|
|
191
|
-
message:
|
|
192
|
-
'userID must be a 1-128 character string composed of letters, numbers, underscores, dots or hyphens',
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
static validateEventType(eventType: any): void {
|
|
197
|
-
const validEvents = [
|
|
198
|
-
'message_received',
|
|
199
|
-
'message_revoked',
|
|
200
|
-
'notification_clicked',
|
|
201
|
-
];
|
|
202
|
-
this.validateValue('eventType', eventType, {
|
|
203
|
-
required: true,
|
|
204
|
-
type: 'string',
|
|
205
|
-
custom: (value) => {
|
|
206
|
-
if (!validEvents.includes(value)) {
|
|
207
|
-
return `eventType must be one of: ${validEvents.join(', ')}`;
|
|
208
|
-
}
|
|
209
|
-
return true;
|
|
210
|
-
},
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
static validateListener(listener: any): void {
|
|
215
|
-
this.validateValue('listener', listener, {
|
|
216
|
-
required: true,
|
|
217
|
-
type: 'function',
|
|
218
|
-
message: 'listener must be a function',
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
static validateRegisterPushOptions(options: any): void {
|
|
223
|
-
if (!options) {
|
|
224
|
-
throw new ValidationError(
|
|
225
|
-
'options',
|
|
226
|
-
'Registration options cannot be empty'
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const schema: ValidationSchema = {
|
|
231
|
-
SDKAppID: {
|
|
232
|
-
required: true,
|
|
233
|
-
type: 'number',
|
|
234
|
-
custom: (value) => {
|
|
235
|
-
if (!Number.isInteger(value)) {
|
|
236
|
-
return 'SDKAppID must be an integer';
|
|
237
|
-
}
|
|
238
|
-
return true;
|
|
239
|
-
},
|
|
240
|
-
},
|
|
241
|
-
appKey: {
|
|
242
|
-
required: true,
|
|
243
|
-
type: 'string',
|
|
244
|
-
pattern: /^[a-zA-Z0-9_-]+$/,
|
|
245
|
-
},
|
|
246
|
-
userID: {
|
|
247
|
-
required: true,
|
|
248
|
-
type: 'string',
|
|
249
|
-
pattern: /^[a-zA-Z0-9_.-]+$/,
|
|
250
|
-
},
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
this.validateObject(options, schema);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Validate Service Worker path
|
|
258
|
-
*/
|
|
259
|
-
static validateServiceWorkerPath(path: any): void {
|
|
260
|
-
this.validateValue('serviceWorkerPath', path, {
|
|
261
|
-
required: false,
|
|
262
|
-
type: 'string',
|
|
263
|
-
pattern: /^[a-zA-Z0-9_./-]+\.js$/,
|
|
264
|
-
message: 'serviceWorkerPath must be a valid JavaScript file path',
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{dist/sw.js → sw.js}
RENAMED
|
File without changes
|