lambder 2.0.2 → 2.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/dist/Lambder.d.ts +5 -5
- package/dist/Lambder.js +3 -2
- package/package.json +1 -1
- package/src/Lambder.ts +10 -9
- package/tests/use-plugin.test.ts +22 -1
package/dist/Lambder.d.ts
CHANGED
|
@@ -115,7 +115,7 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
|
|
|
115
115
|
private handleNoMatchedAction;
|
|
116
116
|
addRoute(condition: Path | ConditionFunction | RegExp, actionFn: ActionFunction): this;
|
|
117
117
|
addSessionRoute(condition: Path | ConditionFunction | RegExp, actionFn: SessionActionFunction<TSessionData>): this;
|
|
118
|
-
use<_TNewContract extends Record<string, any>>(plugin: (lambder: Lambder<TSessionData, _TContract>) => Lambder<TSessionData, _TNewContract>): Lambder<TSessionData, _TNewContract>;
|
|
118
|
+
use<_TNewContract extends Record<string, any>>(plugin: (lambder: Lambder<TSessionData, _TContract>) => Lambder<TSessionData, _TNewContract>): Lambder<TSessionData, _TNewContract extends _TContract ? _TNewContract : (_TContract & _TNewContract)>;
|
|
119
119
|
addApi<TName extends string, TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny>(name: TName, schema: {
|
|
120
120
|
input: TInput;
|
|
121
121
|
output: TOutput;
|
|
@@ -124,10 +124,10 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
|
|
|
124
124
|
input: TInput;
|
|
125
125
|
output: TOutput;
|
|
126
126
|
}, handler: (ctx: LambderSessionRenderContext<z.infer<TInput>, TSessionData>, resolver: LambderResolver<z.infer<TOutput>>) => LambderResolverResponse | Promise<LambderResolverResponse>): Lambder<TSessionData, MergeContract<_TContract, TName, z.infer<TInput>, z.infer<TOutput>>>;
|
|
127
|
-
addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<
|
|
128
|
-
addHook(hookEvent: 'beforeRender', hookFn: HookBeforeRenderFunction, priority?: number):
|
|
129
|
-
addHook(hookEvent: 'afterRender', hookFn: HookAfterRenderFunction, priority?: number):
|
|
130
|
-
addHook(hookEvent: 'fallback', hookFn: HookFallbackFunction, priority?: number):
|
|
127
|
+
addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<this>;
|
|
128
|
+
addHook(hookEvent: 'beforeRender', hookFn: HookBeforeRenderFunction, priority?: number): this;
|
|
129
|
+
addHook(hookEvent: 'afterRender', hookFn: HookAfterRenderFunction, priority?: number): this;
|
|
130
|
+
addHook(hookEvent: 'fallback', hookFn: HookFallbackFunction, priority?: number): this;
|
|
131
131
|
getSessionController(ctx: LambderRenderContext<any> | LambderSessionRenderContext<any, TSessionData>): LambderSessionController<TSessionData>;
|
|
132
132
|
getResponseBuilder(): LambderResponseBuilder<any>;
|
|
133
133
|
private getResolver;
|
package/dist/Lambder.js
CHANGED
|
@@ -250,13 +250,14 @@ export default class Lambder {
|
|
|
250
250
|
});
|
|
251
251
|
return this;
|
|
252
252
|
}
|
|
253
|
-
|
|
253
|
+
addHook(hookEvent, hookFn, priority = 0) {
|
|
254
254
|
if (hookEvent === "created") {
|
|
255
|
-
|
|
255
|
+
return hookFn(this).then(() => this);
|
|
256
256
|
}
|
|
257
257
|
else {
|
|
258
258
|
this.hookList[hookEvent].push({ priority, hookFn });
|
|
259
259
|
this.hookList[hookEvent].sort((a, b) => a.priority - b.priority);
|
|
260
|
+
return this;
|
|
260
261
|
}
|
|
261
262
|
}
|
|
262
263
|
getSessionController(ctx) {
|
package/package.json
CHANGED
package/src/Lambder.ts
CHANGED
|
@@ -285,8 +285,8 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
|
|
|
285
285
|
// Plugin system
|
|
286
286
|
public use<_TNewContract extends Record<string, any>>(
|
|
287
287
|
plugin: (lambder: Lambder<TSessionData, _TContract>) => Lambder<TSessionData, _TNewContract>
|
|
288
|
-
): Lambder<TSessionData, _TNewContract> {
|
|
289
|
-
return plugin(this);
|
|
288
|
+
): Lambder<TSessionData, _TNewContract extends _TContract ? _TNewContract : (_TContract & _TNewContract)> {
|
|
289
|
+
return plugin(this) as any;
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
// Typed API with Zod
|
|
@@ -361,20 +361,21 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
|
|
|
361
361
|
return this as any;
|
|
362
362
|
}
|
|
363
363
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
364
|
+
addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<this>;
|
|
365
|
+
addHook(hookEvent: 'beforeRender', hookFn: HookBeforeRenderFunction, priority?: number): this;
|
|
366
|
+
addHook(hookEvent: 'afterRender', hookFn: HookAfterRenderFunction, priority?: number): this;
|
|
367
|
+
addHook(hookEvent: 'fallback', hookFn: HookFallbackFunction, priority?: number): this;
|
|
368
|
+
addHook(
|
|
369
369
|
hookEvent:HookEventType,
|
|
370
370
|
hookFn: HookCreatedFunction & HookBeforeRenderFunction & HookAfterRenderFunction & HookFallbackFunction,
|
|
371
371
|
priority = 0
|
|
372
|
-
): Promise<
|
|
372
|
+
): this | Promise<this> {
|
|
373
373
|
if(hookEvent === "created"){
|
|
374
|
-
|
|
374
|
+
return hookFn(this).then(() => this);
|
|
375
375
|
}else{
|
|
376
376
|
this.hookList[hookEvent].push({ priority, hookFn });
|
|
377
377
|
this.hookList[hookEvent].sort((a, b) => a.priority - b.priority);
|
|
378
|
+
return this;
|
|
378
379
|
}
|
|
379
380
|
}
|
|
380
381
|
|
package/tests/use-plugin.test.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This file tests the plugin system that allows modular API composition
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { describe, it, expect } from 'vitest';
|
|
7
|
+
import { describe, it, expect, expectTypeOf } from 'vitest';
|
|
8
8
|
import { z } from 'zod';
|
|
9
9
|
import Lambder from '../src/Lambder.js';
|
|
10
10
|
import LambderCaller from '../src/LambderCaller.js';
|
|
@@ -435,3 +435,24 @@ describe('Plugin System - Type Safety', () => {
|
|
|
435
435
|
expect(caller).toBeDefined();
|
|
436
436
|
});
|
|
437
437
|
});
|
|
438
|
+
|
|
439
|
+
// ============================================================================
|
|
440
|
+
// Test 7: Non-Generic Plugin Type Accumulation
|
|
441
|
+
// ============================================================================
|
|
442
|
+
|
|
443
|
+
describe('Plugin System - Non-Generic Plugins', () => {
|
|
444
|
+
it('should accumulate types correctly when using non-generic plugins', () => {
|
|
445
|
+
const plugin1 = (l: Lambder) => l.addApi('api1', { input: z.void(), output: z.void() }, async (ctx, res) => res.raw({ statusCode: 200, body: '' }));
|
|
446
|
+
const plugin2 = (l: Lambder) => l.addApi('api2', { input: z.void(), output: z.void() }, async (ctx, res) => res.raw({ statusCode: 200, body: '' }));
|
|
447
|
+
|
|
448
|
+
const lambder = new Lambder({ publicPath: '', apiPath: '' })
|
|
449
|
+
.use(plugin1)
|
|
450
|
+
.use(plugin2);
|
|
451
|
+
|
|
452
|
+
type Contract = typeof lambder.ApiContractType;
|
|
453
|
+
|
|
454
|
+
// Check if both api1 and api2 exist in Contract
|
|
455
|
+
expectTypeOf<Contract>().toHaveProperty('api1');
|
|
456
|
+
expectTypeOf<Contract>().toHaveProperty('api2');
|
|
457
|
+
});
|
|
458
|
+
});
|