Skip to content

Commit

Permalink
feat(easy): Add wrap function that takes a async function and return …
Browse files Browse the repository at this point in the history
…that result of that function
  • Loading branch information
nlamarti committed Jul 28, 2023
1 parent 48f40b4 commit 292ba7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/easy/src/types/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface RequestContext {
get<T>(key: string): T;
set<T>(key: string, value: T): T;
create: (f: () => void) => void;
wrap<T>(f: () => Promise<T>): Promise<T>;
}

export class BaseRequestContext implements RequestContext {
Expand Down Expand Up @@ -88,6 +89,8 @@ export class BaseRequestContext implements RequestContext {
}

public readonly create = (f: () => void): void => f();

public readonly wrap = <T>(f: () => Promise<T>): Promise<T> => f();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/easy/test/types/Context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe('Environment Context', () => {
expect(ctx.request.lastError).toBe('Wrong');
});

test('simple wrap on request context', async () => {
const r = await ctx.request.wrap(async () => Promise.resolve(42));
expect(r).toBe(42);
});

test('token should be an any in the context', () => {
const c = new Context({ request: new BaseRequestContext() });
c.request.token = { tenant: 42 };
Expand Down

0 comments on commit 292ba7c

Please sign in to comment.