saas-backend-kit 1.0.0
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 +31 -0
- package/PUBLISHING.md +133 -0
- package/README.md +459 -0
- package/copy-dts.js +255 -0
- package/dist/auth/index.d.ts +58 -0
- package/dist/auth/index.js +584 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/index.mjs +569 -0
- package/dist/auth/index.mjs.map +1 -0
- package/dist/config/index.d.ts +22 -0
- package/dist/config/index.js +106 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/index.mjs +100 -0
- package/dist/config/index.mjs.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +1303 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1281 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger/index.d.ts +18 -0
- package/dist/logger/index.js +188 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/index.mjs +178 -0
- package/dist/logger/index.mjs.map +1 -0
- package/dist/notifications/index.d.ts +35 -0
- package/dist/notifications/index.js +339 -0
- package/dist/notifications/index.js.map +1 -0
- package/dist/notifications/index.mjs +328 -0
- package/dist/notifications/index.mjs.map +1 -0
- package/dist/queue/index.d.ts +33 -0
- package/dist/queue/index.js +306 -0
- package/dist/queue/index.js.map +1 -0
- package/dist/queue/index.mjs +293 -0
- package/dist/queue/index.mjs.map +1 -0
- package/dist/rate-limit/index.d.ts +11 -0
- package/dist/rate-limit/index.js +290 -0
- package/dist/rate-limit/index.js.map +1 -0
- package/dist/rate-limit/index.mjs +286 -0
- package/dist/rate-limit/index.mjs.map +1 -0
- package/dist/response/index.d.ts +29 -0
- package/dist/response/index.js +120 -0
- package/dist/response/index.js.map +1 -0
- package/dist/response/index.mjs +114 -0
- package/dist/response/index.mjs.map +1 -0
- package/examples/express/.env.example +41 -0
- package/examples/express/app.ts +203 -0
- package/package.json +109 -0
- package/src/auth/express.ts +250 -0
- package/src/auth/fastify.ts +65 -0
- package/src/auth/index.ts +6 -0
- package/src/auth/jwt.ts +47 -0
- package/src/auth/oauth.ts +117 -0
- package/src/auth/rbac.ts +82 -0
- package/src/auth/types.ts +69 -0
- package/src/config/index.ts +120 -0
- package/src/index.ts +16 -0
- package/src/logger/index.ts +110 -0
- package/src/notifications/index.ts +262 -0
- package/src/plugin.ts +192 -0
- package/src/queue/index.ts +208 -0
- package/src/rate-limit/express.ts +144 -0
- package/src/rate-limit/fastify.ts +47 -0
- package/src/rate-limit/index.ts +2 -0
- package/src/response/index.ts +197 -0
- package/src/utils/index.ts +180 -0
- package/tsconfig.json +30 -0
- package/tsup.config.ts +24 -0
package/copy-dts.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const typeDefs = {
|
|
5
|
+
'auth/index.d.ts': `import { RequestHandler } from 'express';
|
|
6
|
+
|
|
7
|
+
export type Role = string;
|
|
8
|
+
export type Permission = string;
|
|
9
|
+
|
|
10
|
+
export interface User {
|
|
11
|
+
id: string;
|
|
12
|
+
email: string;
|
|
13
|
+
password?: string;
|
|
14
|
+
role: Role;
|
|
15
|
+
name?: string;
|
|
16
|
+
picture?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface JWTPayload {
|
|
20
|
+
userId: string;
|
|
21
|
+
email: string;
|
|
22
|
+
role: Role;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TokenPair {
|
|
26
|
+
accessToken: string;
|
|
27
|
+
refreshToken: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface AuthOptions {
|
|
31
|
+
jwtSecret?: string;
|
|
32
|
+
jwtExpiresIn?: string;
|
|
33
|
+
refreshSecret?: string;
|
|
34
|
+
refreshExpiresIn?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LoginCredentials {
|
|
38
|
+
email: string;
|
|
39
|
+
password: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RegisterData extends LoginCredentials {
|
|
43
|
+
name?: string;
|
|
44
|
+
role?: Role;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export declare class AuthService {
|
|
48
|
+
initialize(): Promise<void>;
|
|
49
|
+
register(data: RegisterData): Promise<{ user: User; tokens: TokenPair }>;
|
|
50
|
+
login(credentials: LoginCredentials): Promise<{ user: User; tokens: TokenPair }>;
|
|
51
|
+
refresh(refreshToken: string): Promise<TokenPair>;
|
|
52
|
+
getGoogleAuthUrl(state?: string): Promise<string>;
|
|
53
|
+
handleGoogleCallback(code: string): Promise<{ user: User; tokens: TokenPair }>;
|
|
54
|
+
getMiddleware(): RequestHandler;
|
|
55
|
+
requireUser(): RequestHandler;
|
|
56
|
+
requireRole(role: string): RequestHandler;
|
|
57
|
+
requirePermission(permission: string): RequestHandler;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export declare function createAuth(options?: AuthOptions): AuthService;
|
|
61
|
+
export declare function auth(): AuthService;
|
|
62
|
+
export declare const Auth: { initialize: (options?: AuthOptions) => any };
|
|
63
|
+
`,
|
|
64
|
+
'queue/index.d.ts': `export interface QueueOptions {
|
|
65
|
+
name: string;
|
|
66
|
+
defaultJobOptions?: any;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface JobData {
|
|
70
|
+
[key: string]: unknown;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type JobProcessor = (job: any) => Promise<unknown>;
|
|
74
|
+
|
|
75
|
+
export interface Queue {
|
|
76
|
+
add(name: string, data: JobData, opts?: any): Promise<any>;
|
|
77
|
+
addBulk(jobs: any[]): Promise<any[]>;
|
|
78
|
+
getJobCounts(): Promise<any>;
|
|
79
|
+
getJobs(statuses: string[], start?: number, end?: number): Promise<any[]>;
|
|
80
|
+
close(): Promise<void>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export declare const createQueue: (name: string, options?: Partial<QueueOptions>) => Queue;
|
|
84
|
+
export declare const addJob: (queueName: string, jobName: string, data: JobData, options?: any) => Promise<any>;
|
|
85
|
+
export declare const processJob: (queueName: string, processor: JobProcessor, options?: any) => any;
|
|
86
|
+
export declare const queue: {
|
|
87
|
+
create: typeof createQueue;
|
|
88
|
+
add: typeof addJob;
|
|
89
|
+
process: typeof processJob;
|
|
90
|
+
get: (name: string) => Queue | undefined;
|
|
91
|
+
getJobCounts: (name: string) => Promise<any>;
|
|
92
|
+
getJobs: (name: string, start?: number, end?: number) => Promise<any[]>;
|
|
93
|
+
close: (name: string) => Promise<void>;
|
|
94
|
+
closeAll: () => Promise<void>;
|
|
95
|
+
setRedisOptions: (options: any) => void;
|
|
96
|
+
};
|
|
97
|
+
`,
|
|
98
|
+
'notifications/index.d.ts': `export interface EmailOptions {
|
|
99
|
+
to: string | string[];
|
|
100
|
+
subject: string;
|
|
101
|
+
text?: string;
|
|
102
|
+
html?: string;
|
|
103
|
+
template?: string;
|
|
104
|
+
templateData?: Record<string, unknown>;
|
|
105
|
+
from?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface SMSOptions {
|
|
109
|
+
to: string;
|
|
110
|
+
message: string;
|
|
111
|
+
from?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface WebhookOptions {
|
|
115
|
+
url: string;
|
|
116
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH';
|
|
117
|
+
headers?: Record<string, string>;
|
|
118
|
+
body?: unknown;
|
|
119
|
+
timeout?: number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface SlackOptions {
|
|
123
|
+
text?: string;
|
|
124
|
+
blocks?: any[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export declare const notify: {
|
|
128
|
+
email: (options: EmailOptions) => Promise<{ messageId: string }>;
|
|
129
|
+
sms: (options: SMSOptions) => Promise<{ sid: string }>;
|
|
130
|
+
webhook: (options: WebhookOptions) => Promise<{ status: number; body: unknown }>;
|
|
131
|
+
slack: (options: SlackOptions) => Promise<{ ok: boolean }>;
|
|
132
|
+
};
|
|
133
|
+
`,
|
|
134
|
+
'logger/index.d.ts': `export type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
135
|
+
|
|
136
|
+
export interface LoggerConfig {
|
|
137
|
+
level?: LogLevel;
|
|
138
|
+
name?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface Logger {
|
|
142
|
+
info(message: string, ...args: any[]): void;
|
|
143
|
+
warn(message: string, ...args: any[]): void;
|
|
144
|
+
error(message: string, ...args: any[]): void;
|
|
145
|
+
debug(message: string, ...args: any[]): void;
|
|
146
|
+
trace(message: string, ...args: any[]): void;
|
|
147
|
+
fatal(message: string, ...args: any[]): void;
|
|
148
|
+
child(bindings: any, options?: { name?: string }): Logger;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export declare const logger: Logger;
|
|
152
|
+
`,
|
|
153
|
+
'rate-limit/index.d.ts': `import { Request, Response } from 'express';
|
|
154
|
+
|
|
155
|
+
export interface RateLimitOptions {
|
|
156
|
+
window?: string;
|
|
157
|
+
limit?: number;
|
|
158
|
+
keyGenerator?: (req: Request) => string;
|
|
159
|
+
handler?: (req: Request, res: Response) => void;
|
|
160
|
+
skip?: (req: Request) => boolean;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export declare function rateLimit(options?: RateLimitOptions): (req: Request, res: Response, next: any) => void;
|
|
164
|
+
`,
|
|
165
|
+
'config/index.d.ts': `export type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
166
|
+
|
|
167
|
+
export interface EnvConfig {
|
|
168
|
+
NODE_ENV: 'development' | 'production' | 'test';
|
|
169
|
+
PORT: string;
|
|
170
|
+
DATABASE_URL?: string;
|
|
171
|
+
REDIS_URL: string;
|
|
172
|
+
JWT_SECRET?: string;
|
|
173
|
+
JWT_EXPIRES_IN?: string;
|
|
174
|
+
[key: string]: any;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export declare const config: {
|
|
178
|
+
load: () => EnvConfig;
|
|
179
|
+
get: <K extends keyof EnvConfig>(key: K) => EnvConfig[K];
|
|
180
|
+
int: (key: keyof EnvConfig) => number;
|
|
181
|
+
bool: (key: keyof EnvConfig) => boolean;
|
|
182
|
+
isProduction: () => boolean;
|
|
183
|
+
isDevelopment: () => boolean;
|
|
184
|
+
isTest: () => boolean;
|
|
185
|
+
getAll: () => EnvConfig;
|
|
186
|
+
};
|
|
187
|
+
`,
|
|
188
|
+
'response/index.d.ts': `import { Response } from 'express';
|
|
189
|
+
|
|
190
|
+
export interface ApiResponse<T = any> {
|
|
191
|
+
success: boolean;
|
|
192
|
+
data?: T;
|
|
193
|
+
error?: string;
|
|
194
|
+
message?: string;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface PaginatedResponse<T> extends ApiResponse<T> {
|
|
198
|
+
meta: { page: number; limit: number; total: number; totalPages: number };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export declare class ResponseHelper {
|
|
202
|
+
static success<T>(res: Response, data?: T, message?: string, statusCode?: number): Response;
|
|
203
|
+
static created<T>(res: Response, data?: T, message?: string): Response;
|
|
204
|
+
static updated<T>(res: Response, data?: T, message?: string): Response;
|
|
205
|
+
static deleted(res: Response, message?: string): Response;
|
|
206
|
+
static error(res: Response, error: string, statusCode?: number): Response;
|
|
207
|
+
static badRequest(res: Response, error?: string): Response;
|
|
208
|
+
static unauthorized(res: Response, error?: string): Response;
|
|
209
|
+
static forbidden(res: Response, error?: string): Response;
|
|
210
|
+
static notFound(res: Response, error?: string): Response;
|
|
211
|
+
static validationError(res: Response, error: string): Response;
|
|
212
|
+
static internalError(res: Response, error?: string): Response;
|
|
213
|
+
static paginated<T>(res: Response, data: T[], page: number, limit: number, total: number): Response;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export declare const response: typeof ResponseHelper;
|
|
217
|
+
`,
|
|
218
|
+
'index.d.ts': `export * from './auth';
|
|
219
|
+
export * from './queue';
|
|
220
|
+
export * from './notifications';
|
|
221
|
+
export * from './logger';
|
|
222
|
+
export * from './rate-limit';
|
|
223
|
+
export * from './config';
|
|
224
|
+
export * from './response';
|
|
225
|
+
|
|
226
|
+
export interface AppOptions {
|
|
227
|
+
framework?: 'express' | 'fastify';
|
|
228
|
+
auth?: boolean | any;
|
|
229
|
+
queue?: boolean | { redisUrl?: string };
|
|
230
|
+
notifications?: boolean;
|
|
231
|
+
rateLimit?: boolean | any;
|
|
232
|
+
logger?: boolean | any;
|
|
233
|
+
config?: boolean;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface Plugin {
|
|
237
|
+
name: string;
|
|
238
|
+
initialize: (app: any) => Promise<void> | void;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export declare function createApp(options?: AppOptions): any;
|
|
242
|
+
export declare function createExpressApp(options?: AppOptions): any;
|
|
243
|
+
`
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
for (const [filePath, content] of Object.entries(typeDefs)) {
|
|
247
|
+
const fullPath = path.join('dist', filePath);
|
|
248
|
+
const dir = path.dirname(fullPath);
|
|
249
|
+
if (!fs.existsSync(dir)) {
|
|
250
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
251
|
+
}
|
|
252
|
+
fs.writeFileSync(fullPath, content);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
console.log('Type definitions written successfully');
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
|
|
3
|
+
export type Role = string;
|
|
4
|
+
export type Permission = string;
|
|
5
|
+
|
|
6
|
+
export interface User {
|
|
7
|
+
id: string;
|
|
8
|
+
email: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
role: Role;
|
|
11
|
+
name?: string;
|
|
12
|
+
picture?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface JWTPayload {
|
|
16
|
+
userId: string;
|
|
17
|
+
email: string;
|
|
18
|
+
role: Role;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface TokenPair {
|
|
22
|
+
accessToken: string;
|
|
23
|
+
refreshToken: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AuthOptions {
|
|
27
|
+
jwtSecret?: string;
|
|
28
|
+
jwtExpiresIn?: string;
|
|
29
|
+
refreshSecret?: string;
|
|
30
|
+
refreshExpiresIn?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface LoginCredentials {
|
|
34
|
+
email: string;
|
|
35
|
+
password: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RegisterData extends LoginCredentials {
|
|
39
|
+
name?: string;
|
|
40
|
+
role?: Role;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export declare class AuthService {
|
|
44
|
+
initialize(): Promise<void>;
|
|
45
|
+
register(data: RegisterData): Promise<{ user: User; tokens: TokenPair }>;
|
|
46
|
+
login(credentials: LoginCredentials): Promise<{ user: User; tokens: TokenPair }>;
|
|
47
|
+
refresh(refreshToken: string): Promise<TokenPair>;
|
|
48
|
+
getGoogleAuthUrl(state?: string): Promise<string>;
|
|
49
|
+
handleGoogleCallback(code: string): Promise<{ user: User; tokens: TokenPair }>;
|
|
50
|
+
getMiddleware(): RequestHandler;
|
|
51
|
+
requireUser(): RequestHandler;
|
|
52
|
+
requireRole(role: string): RequestHandler;
|
|
53
|
+
requirePermission(permission: string): RequestHandler;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export declare function createAuth(options?: AuthOptions): AuthService;
|
|
57
|
+
export declare function auth(): AuthService;
|
|
58
|
+
export declare const Auth: { initialize: (options?: AuthOptions) => any };
|