@bluemarble/bm-components 2.2.0 → 2.3.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/dist/index.cjs +44 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -9
- package/dist/index.d.ts +19 -9
- package/dist/index.js +32 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -434,25 +434,35 @@ declare class DomainError extends Error {
|
|
|
434
434
|
}
|
|
435
435
|
|
|
436
436
|
type PossibleMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'ALL';
|
|
437
|
-
type
|
|
438
|
-
user?:
|
|
439
|
-
}
|
|
437
|
+
type NextHandlerApiRequest<T = string> = NextApiRequest & {
|
|
438
|
+
user?: T;
|
|
439
|
+
};
|
|
440
|
+
type HandlerProps<UserKeyType> = (req: NextHandlerApiRequest<UserKeyType>, res: NextApiResponse, options?: TApiOptions) => Promise<any>;
|
|
440
441
|
type MethodProps<T> = Partial<Record<PossibleMethods, HandlerProps<T>>>;
|
|
441
|
-
type MiddlewaresProps<T> = ((handler: HandlerProps<T
|
|
442
|
+
type MiddlewaresProps<T> = ((handler: HandlerProps<T>, options?: TApiOptions) => HandlerProps<T>)[];
|
|
442
443
|
type ConstructorProps$1<T> = {
|
|
444
|
+
public?: boolean;
|
|
443
445
|
middlewares?: MiddlewaresProps<T>;
|
|
444
446
|
onFinally?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
445
447
|
onError?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
446
448
|
};
|
|
449
|
+
type TApiOptions = {
|
|
450
|
+
public?: boolean;
|
|
451
|
+
middlewares?: MiddlewaresProps<any>;
|
|
452
|
+
};
|
|
447
453
|
declare class ApiHelper<T> {
|
|
448
|
-
middlewares
|
|
454
|
+
private middlewares;
|
|
455
|
+
private public;
|
|
449
456
|
private onFinally;
|
|
450
457
|
private onError;
|
|
451
458
|
constructor(props?: ConstructorProps$1<T>);
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
static
|
|
455
|
-
static
|
|
459
|
+
createMethods(methods: MethodProps<T>): (req: NextApiRequest, res: NextApiResponse) => Promise<any>;
|
|
460
|
+
private buildFactory;
|
|
461
|
+
static build(factory: (req: NextHandlerApiRequest, res: NextApiResponse) => MethodProps<string>, options?: TApiOptions): (req: NextHandlerApiRequest, res: NextApiResponse) => Promise<any>;
|
|
462
|
+
static parser<Output, Def>(body: Record<string, any>, parser: ZodSchema<Output, Def>): Output;
|
|
463
|
+
/** @deprecated Use {@link ApiHelper.parser} instead. */
|
|
464
|
+
static parserErrorWrapper: typeof ApiHelper.parser;
|
|
465
|
+
/** @deprecated Use {@Link ApiHelper.build} instead. */
|
|
456
466
|
static create({ onFinally }: {
|
|
457
467
|
onFinally: any;
|
|
458
468
|
}): ApiHelper<unknown>;
|
package/dist/index.d.ts
CHANGED
|
@@ -434,25 +434,35 @@ declare class DomainError extends Error {
|
|
|
434
434
|
}
|
|
435
435
|
|
|
436
436
|
type PossibleMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'ALL';
|
|
437
|
-
type
|
|
438
|
-
user?:
|
|
439
|
-
}
|
|
437
|
+
type NextHandlerApiRequest<T = string> = NextApiRequest & {
|
|
438
|
+
user?: T;
|
|
439
|
+
};
|
|
440
|
+
type HandlerProps<UserKeyType> = (req: NextHandlerApiRequest<UserKeyType>, res: NextApiResponse, options?: TApiOptions) => Promise<any>;
|
|
440
441
|
type MethodProps<T> = Partial<Record<PossibleMethods, HandlerProps<T>>>;
|
|
441
|
-
type MiddlewaresProps<T> = ((handler: HandlerProps<T
|
|
442
|
+
type MiddlewaresProps<T> = ((handler: HandlerProps<T>, options?: TApiOptions) => HandlerProps<T>)[];
|
|
442
443
|
type ConstructorProps$1<T> = {
|
|
444
|
+
public?: boolean;
|
|
443
445
|
middlewares?: MiddlewaresProps<T>;
|
|
444
446
|
onFinally?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
445
447
|
onError?: (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
446
448
|
};
|
|
449
|
+
type TApiOptions = {
|
|
450
|
+
public?: boolean;
|
|
451
|
+
middlewares?: MiddlewaresProps<any>;
|
|
452
|
+
};
|
|
447
453
|
declare class ApiHelper<T> {
|
|
448
|
-
middlewares
|
|
454
|
+
private middlewares;
|
|
455
|
+
private public;
|
|
449
456
|
private onFinally;
|
|
450
457
|
private onError;
|
|
451
458
|
constructor(props?: ConstructorProps$1<T>);
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
static
|
|
455
|
-
static
|
|
459
|
+
createMethods(methods: MethodProps<T>): (req: NextApiRequest, res: NextApiResponse) => Promise<any>;
|
|
460
|
+
private buildFactory;
|
|
461
|
+
static build(factory: (req: NextHandlerApiRequest, res: NextApiResponse) => MethodProps<string>, options?: TApiOptions): (req: NextHandlerApiRequest, res: NextApiResponse) => Promise<any>;
|
|
462
|
+
static parser<Output, Def>(body: Record<string, any>, parser: ZodSchema<Output, Def>): Output;
|
|
463
|
+
/** @deprecated Use {@link ApiHelper.parser} instead. */
|
|
464
|
+
static parserErrorWrapper: typeof ApiHelper.parser;
|
|
465
|
+
/** @deprecated Use {@Link ApiHelper.build} instead. */
|
|
456
466
|
static create({ onFinally }: {
|
|
457
467
|
onFinally: any;
|
|
458
468
|
}): ApiHelper<unknown>;
|
package/dist/index.js
CHANGED
|
@@ -4501,55 +4501,62 @@ var VALID_METHODS = [
|
|
|
4501
4501
|
"PATCH"
|
|
4502
4502
|
];
|
|
4503
4503
|
var _ApiHelper = class _ApiHelper {
|
|
4504
|
-
async onFinally(
|
|
4504
|
+
async onFinally(_req, _res) {
|
|
4505
4505
|
}
|
|
4506
|
-
async onError(
|
|
4506
|
+
async onError(_req, _res, _error) {
|
|
4507
4507
|
}
|
|
4508
4508
|
constructor(props) {
|
|
4509
|
+
this.public = props?.public ?? false;
|
|
4509
4510
|
this.middlewares = (props?.middlewares || []).reverse();
|
|
4510
4511
|
this.onFinally = props?.onFinally || (async () => {
|
|
4511
4512
|
});
|
|
4512
4513
|
this.onError = props?.onError || (async () => {
|
|
4513
4514
|
});
|
|
4514
4515
|
}
|
|
4515
|
-
setMiddlewares(middlewares) {
|
|
4516
|
-
this.middlewares = middlewares.reverse();
|
|
4517
|
-
return this;
|
|
4518
|
-
}
|
|
4519
4516
|
createMethods(methods) {
|
|
4520
4517
|
return async (req, res) => {
|
|
4521
4518
|
const currentMethod = methods[req.method] || methods.ALL;
|
|
4519
|
+
const options = { public: this.public };
|
|
4522
4520
|
if (req.method === "OPTIONS") return res.status(200).end();
|
|
4523
4521
|
try {
|
|
4524
4522
|
if (!VALID_METHODS.includes(req.method))
|
|
4525
4523
|
throw new HttpError(405, "M\xE9todo inv\xE1lido");
|
|
4526
4524
|
if (!currentMethod) throw new HttpError(500, "M\xE9todo n\xE3o encontrado");
|
|
4527
4525
|
const methodWithMiddlewares = this.middlewares.reduce(
|
|
4528
|
-
(acc, fn) => fn(acc),
|
|
4526
|
+
(acc, fn) => fn(acc, options),
|
|
4529
4527
|
currentMethod
|
|
4530
4528
|
);
|
|
4531
|
-
await methodWithMiddlewares(req, res);
|
|
4529
|
+
return await methodWithMiddlewares(req, res, options);
|
|
4532
4530
|
} catch (error) {
|
|
4533
|
-
if (error instanceof DomainError)
|
|
4534
|
-
|
|
4535
|
-
if (error instanceof HttpError)
|
|
4536
|
-
return res.status(error.status).json(error.message);
|
|
4537
|
-
if (process.env.NODE_ENV === "production") {
|
|
4538
|
-
res.status(500).json({
|
|
4539
|
-
code: "internal.error",
|
|
4540
|
-
error: "Erro interno do servidor",
|
|
4541
|
-
details: error
|
|
4542
|
-
});
|
|
4543
|
-
return this.onError(req, res, error);
|
|
4544
|
-
}
|
|
4531
|
+
if (error instanceof DomainError) return res.status(400).json(error.message);
|
|
4532
|
+
if (error instanceof HttpError) return res.status(error.status).json(error.message);
|
|
4545
4533
|
this.onError(req, res, error);
|
|
4546
|
-
throw
|
|
4534
|
+
throw error;
|
|
4547
4535
|
} finally {
|
|
4548
4536
|
await this.onFinally(req, res);
|
|
4549
4537
|
}
|
|
4550
4538
|
};
|
|
4551
4539
|
}
|
|
4552
|
-
|
|
4540
|
+
buildFactory(factory) {
|
|
4541
|
+
const options = {
|
|
4542
|
+
public: this.public
|
|
4543
|
+
};
|
|
4544
|
+
return async (req, res) => {
|
|
4545
|
+
const methods = factory(req, res);
|
|
4546
|
+
const handler = methods[req.method];
|
|
4547
|
+
const methodWithMiddlewares = this.middlewares.reduce((acc, fn) => {
|
|
4548
|
+
return fn(acc, options);
|
|
4549
|
+
}, handler);
|
|
4550
|
+
return await methodWithMiddlewares(req, res, options);
|
|
4551
|
+
};
|
|
4552
|
+
}
|
|
4553
|
+
static build(factory, options) {
|
|
4554
|
+
const helper = new _ApiHelper({
|
|
4555
|
+
...options
|
|
4556
|
+
}).buildFactory(factory);
|
|
4557
|
+
return helper;
|
|
4558
|
+
}
|
|
4559
|
+
static parser(body, parser) {
|
|
4553
4560
|
try {
|
|
4554
4561
|
const object = parser.parse(body);
|
|
4555
4562
|
return object;
|
|
@@ -4561,13 +4568,15 @@ var _ApiHelper = class _ApiHelper {
|
|
|
4561
4568
|
});
|
|
4562
4569
|
}
|
|
4563
4570
|
}
|
|
4571
|
+
/** @deprecated Use {@Link ApiHelper.build} instead. */
|
|
4564
4572
|
static create({ onFinally }) {
|
|
4565
4573
|
return new _ApiHelper({
|
|
4566
4574
|
onFinally
|
|
4567
4575
|
});
|
|
4568
4576
|
}
|
|
4569
4577
|
};
|
|
4570
|
-
|
|
4578
|
+
/** @deprecated Use {@link ApiHelper.parser} instead. */
|
|
4579
|
+
_ApiHelper.parserErrorWrapper = _ApiHelper.parser;
|
|
4571
4580
|
var ApiHelper = _ApiHelper;
|
|
4572
4581
|
|
|
4573
4582
|
// src/hooks/useFormHelper.ts
|