-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixed-pipes.ts
69 lines (58 loc) · 1.89 KB
/
mixed-pipes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {take} from 'take-n-pipe';
interface CreateUserEvent {
name: string;
email: string;
}
const inputData: unknown = {
name: 'John Doe',
email: 'john.doe@example.com'
};
function log(..._input: unknown[]): void {
}
class db {
static createUser(_user: CreateUserEvent): Promise<string> {
return Promise.resolve('123-random-user-id-456');
}
}
class cache {
static putUser(user: CreateUserEvent & {id: string}): Promise<string> {
return Promise.resolve(user.id);
}
}
test('Example: mixed pipes', async () => {
// 1. Take CreateUserEvent as input
const userId: string = await take(inputData)
// 2. Validate data
.pipe((event: unknown) => {
if(typeof event !== 'object' || event === null)
throw new Error('Invalid request event');
if(!('name' in event) || typeof event.name !== 'string')
throw new Error('Event object is missing `name` field of type `string`');
if(!('email' in event) || typeof event.email !== 'string')
throw new Error('Event object is missing `email` field of type `string`');
return event as CreateUserEvent;
})
// 3. Sanitize data
.pipe((event: CreateUserEvent) => {
event.name = event.name.trim();
event.email = event.name.trim();
return event;
})
// 4. Log data
.pipe((event: CreateUserEvent) => {
log(`Creating user ${event.name}...`);
return event;
})
// 5. Create new user in the database
// Here we are switch to async context. Remember: this is a one-way ticket.
.pipeAsync(async (event: CreateUserEvent) => {
const userId: string = await db.createUser(event);
return {...event, id: userId};
})
// 6. Popualte cache
// You can also pass a function directly if its declaration matches.
.pipeAsync(cache.putUser)
// Obtain results as a Promise
.toPromise();
expect(userId).toBe('123-random-user-id-456');
});