@productminds/article-events 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/index.ts +3 -84
- package/package.json +1 -1
- package/src/events/ArticleEvent.ts +19 -0
- package/src/events/ExternalArticleEvent.ts +19 -0
- package/src/events/InternalArticleEvent.ts +19 -0
- package/src/types/Article.ts +46 -0
- package/src/utils/makeHelpers.ts +55 -0
package/index.ts
CHANGED
@@ -1,84 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
jsonSchema: { format: 'date-time' }
|
5
|
-
});
|
6
|
-
|
7
|
-
const CategoryTagSchema = Schema.Struct({
|
8
|
-
type: Schema.Literal('category'),
|
9
|
-
value: Schema.NonEmpty
|
10
|
-
});
|
11
|
-
|
12
|
-
const ArticleReferenceSchema = Schema.Struct({
|
13
|
-
type: Schema.Literal('articleReference'),
|
14
|
-
id: Schema.UUID
|
15
|
-
});
|
16
|
-
|
17
|
-
const TagSchema = Schema.Union(CategoryTagSchema, ArticleReferenceSchema);
|
18
|
-
|
19
|
-
const AuthorSchema = Schema.Struct({
|
20
|
-
name: Schema.NonEmpty
|
21
|
-
});
|
22
|
-
|
23
|
-
export const ArticleSchema = Schema.Struct({
|
24
|
-
id: Schema.UUID,
|
25
|
-
title: Schema.NonEmpty,
|
26
|
-
teaser: Schema.NonEmpty,
|
27
|
-
content: Schema.NonEmpty,
|
28
|
-
publishedAt: DateTimeSchema,
|
29
|
-
updatedAt: DateTimeSchema,
|
30
|
-
retrievedAt: DateTimeSchema,
|
31
|
-
url: Schema.NonEmpty,
|
32
|
-
site: Schema.NonEmpty,
|
33
|
-
tags: Schema.Array(TagSchema),
|
34
|
-
authors: Schema.Array(AuthorSchema),
|
35
|
-
meta: Schema.optional(Schema.Unknown)
|
36
|
-
});
|
37
|
-
|
38
|
-
export const ArticleEventSchema = Schema.Struct({
|
39
|
-
kind: Schema.Literal('ARTICLE_EVENT'),
|
40
|
-
payload: Schema.Struct({
|
41
|
-
article: ArticleSchema,
|
42
|
-
status: Schema.Literal('NEW', 'UPDATED'),
|
43
|
-
source: Schema.Literal('INTERNAL', 'EXTERNAL')
|
44
|
-
})
|
45
|
-
});
|
46
|
-
|
47
|
-
export const decode = Schema.decodeUnknownEither(ArticleEventSchema);
|
48
|
-
export const decodeExn = (u: unknown) => {
|
49
|
-
const decoded = decode(u);
|
50
|
-
|
51
|
-
if (decoded._tag === 'Left') {
|
52
|
-
throw new Error('Failed to decode ArticleEvent');
|
53
|
-
}
|
54
|
-
|
55
|
-
return decoded.right;
|
56
|
-
};
|
57
|
-
|
58
|
-
export const encode = Schema.encodeEither(ArticleEventSchema);
|
59
|
-
export const encodeExn = (event: typeof ArticleEventSchema.Type) => {
|
60
|
-
const encoded = encode(event);
|
61
|
-
|
62
|
-
if (encoded._tag === 'Left') {
|
63
|
-
throw new Error('Failed to encode ArticleEvent');
|
64
|
-
}
|
65
|
-
|
66
|
-
return encoded.right;
|
67
|
-
};
|
68
|
-
|
69
|
-
export const fromString = (msg: string) => decode(JSON.parse(msg));
|
70
|
-
export const fromBuffer = (msg: Buffer) => decode(JSON.parse(msg.toString()));
|
71
|
-
|
72
|
-
export const fromStringExn = (msg: string) => decodeExn(JSON.parse(msg));
|
73
|
-
export const fromBufferExn = (msg: Buffer) => fromStringExn(msg.toString());
|
74
|
-
|
75
|
-
export const toString = (event: typeof ArticleEventSchema.Type) =>
|
76
|
-
JSON.stringify(encode(event));
|
77
|
-
|
78
|
-
export const toBuffer = (event: typeof ArticleEventSchema.Type) =>
|
79
|
-
Buffer.from(toString(event));
|
80
|
-
|
81
|
-
export const toStringExn = (event: typeof ArticleEventSchema.Type) =>
|
82
|
-
JSON.stringify(encodeExn(event));
|
83
|
-
export const toBufferExn = (event: typeof ArticleEventSchema.Type) =>
|
84
|
-
Buffer.from(toStringExn(event));
|
1
|
+
export * from './src/events/ArticleEvent';
|
2
|
+
export * from './src/events/ExternalArticleEvent';
|
3
|
+
export * from './src/events/InternalArticleEvent';
|
package/package.json
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
import { Schema } from '@effect/schema';
|
2
|
+
import { ArticleSchema } from '../types/Article';
|
3
|
+
|
4
|
+
export const ArticleEventSchema = Schema.Struct({
|
5
|
+
kind: Schema.Literal('ARTICLE_EVENT'),
|
6
|
+
payload: Schema.Struct({
|
7
|
+
article: ArticleSchema,
|
8
|
+
status: Schema.Literal('NEW', 'UPDATED'),
|
9
|
+
source: Schema.Literal('INTERNAL', 'EXTERNAL')
|
10
|
+
})
|
11
|
+
});
|
12
|
+
|
13
|
+
import { makeHelpers } from '../utils/makeHelpers';
|
14
|
+
const helpers = makeHelpers(ArticleEventSchema);
|
15
|
+
|
16
|
+
export default {
|
17
|
+
Schema: ArticleEventSchema,
|
18
|
+
...makeHelpers
|
19
|
+
};
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { Schema } from '@effect/schema';
|
2
|
+
import { ArticleSchema } from '../types/Article';
|
3
|
+
|
4
|
+
export const ExternalArticleEventSchema = Schema.Struct({
|
5
|
+
kind: Schema.Literal('EXTERNAL_ARTICLE_EVENT'),
|
6
|
+
payload: Schema.Struct({
|
7
|
+
article: ArticleSchema,
|
8
|
+
status: Schema.Literal('NEW', 'UPDATED'),
|
9
|
+
source: Schema.Literal('EXTERNAL')
|
10
|
+
})
|
11
|
+
});
|
12
|
+
|
13
|
+
import { makeHelpers } from '../utils/makeHelpers';
|
14
|
+
const helpers = makeHelpers(ExternalArticleEventSchema);
|
15
|
+
|
16
|
+
export default {
|
17
|
+
Schema: ExternalArticleEventSchema,
|
18
|
+
...makeHelpers
|
19
|
+
};
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { Schema } from '@effect/schema';
|
2
|
+
import { ArticleSchema } from '../types/Article';
|
3
|
+
|
4
|
+
export const InternalArticleEvent = Schema.Struct({
|
5
|
+
kind: Schema.Literal('INTERNAL_ARTICLE_EVENT'),
|
6
|
+
payload: Schema.Struct({
|
7
|
+
article: ArticleSchema,
|
8
|
+
status: Schema.Literal('NEW', 'UPDATED'),
|
9
|
+
source: Schema.Literal('INTERNAL')
|
10
|
+
})
|
11
|
+
});
|
12
|
+
|
13
|
+
import { makeHelpers } from '../utils/makeHelpers';
|
14
|
+
const helpers = makeHelpers(InternalArticleEvent);
|
15
|
+
|
16
|
+
export default {
|
17
|
+
Schema: InternalArticleEvent,
|
18
|
+
...makeHelpers
|
19
|
+
};
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { Schema } from '@effect/schema';
|
2
|
+
|
3
|
+
export const DateTimeSchema = Schema.Date.annotations({
|
4
|
+
jsonSchema: { format: 'date-time' }
|
5
|
+
});
|
6
|
+
|
7
|
+
export const CategoryTagSchema = Schema.Struct({
|
8
|
+
type: Schema.Literal('CATEGORY'),
|
9
|
+
value: Schema.NonEmpty
|
10
|
+
});
|
11
|
+
|
12
|
+
export const ArticleReferenceSchema = Schema.Struct({
|
13
|
+
type: Schema.Literal('EXTERNAL_ARTICLE_REFERENCE'),
|
14
|
+
id: Schema.UUID
|
15
|
+
});
|
16
|
+
|
17
|
+
export const EntityTag = Schema.Struct({
|
18
|
+
type: Schema.Literal('ENTITY'),
|
19
|
+
entityType: Schema.Literal('PERSON'),
|
20
|
+
value: Schema.NonEmpty
|
21
|
+
});
|
22
|
+
|
23
|
+
export const TagSchema = Schema.Union(
|
24
|
+
CategoryTagSchema,
|
25
|
+
ArticleReferenceSchema,
|
26
|
+
EntityTag
|
27
|
+
);
|
28
|
+
|
29
|
+
export const AuthorSchema = Schema.Struct({
|
30
|
+
name: Schema.NonEmpty
|
31
|
+
});
|
32
|
+
|
33
|
+
export const ArticleSchema = Schema.Struct({
|
34
|
+
id: Schema.UUID,
|
35
|
+
title: Schema.NonEmpty,
|
36
|
+
teaser: Schema.NonEmpty,
|
37
|
+
content: Schema.NonEmpty,
|
38
|
+
publishedAt: DateTimeSchema,
|
39
|
+
updatedAt: DateTimeSchema,
|
40
|
+
retrievedAt: DateTimeSchema,
|
41
|
+
url: Schema.NonEmpty,
|
42
|
+
site: Schema.NonEmpty,
|
43
|
+
tags: Schema.Array(TagSchema),
|
44
|
+
authors: Schema.Array(AuthorSchema),
|
45
|
+
meta: Schema.optional(Schema.Unknown)
|
46
|
+
});
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import { Schema } from '@effect/schema';
|
2
|
+
|
3
|
+
export const makeHelpers = <A, I>(schema: Schema.Schema<A, I, never>) => {
|
4
|
+
const decode = Schema.decodeUnknownEither(schema);
|
5
|
+
const decodeExn = (u: unknown) => {
|
6
|
+
const decoded = decode(u);
|
7
|
+
|
8
|
+
if (decoded._tag === 'Left') {
|
9
|
+
throw new Error('Failed to decode ArticleEvent');
|
10
|
+
}
|
11
|
+
|
12
|
+
return decoded.right;
|
13
|
+
};
|
14
|
+
|
15
|
+
const encode = Schema.encodeEither(schema);
|
16
|
+
const encodeExn = (event: typeof schema.Type) => {
|
17
|
+
const encoded = encode(event);
|
18
|
+
|
19
|
+
if (encoded._tag === 'Left') {
|
20
|
+
throw new Error('Failed to encode ArticleEvent');
|
21
|
+
}
|
22
|
+
|
23
|
+
return encoded.right;
|
24
|
+
};
|
25
|
+
|
26
|
+
const fromString = (msg: string) => decode(JSON.parse(msg));
|
27
|
+
const fromBuffer = (msg: Buffer) => decode(JSON.parse(msg.toString()));
|
28
|
+
|
29
|
+
const fromStringExn = (msg: string) => decodeExn(JSON.parse(msg));
|
30
|
+
const fromBufferExn = (msg: Buffer) => fromStringExn(msg.toString());
|
31
|
+
|
32
|
+
const toString = (event: typeof schema.Type) => JSON.stringify(encode(event));
|
33
|
+
|
34
|
+
const toBuffer = (event: typeof schema.Type) => Buffer.from(toString(event));
|
35
|
+
|
36
|
+
const toStringExn = (event: typeof schema.Type) =>
|
37
|
+
JSON.stringify(encodeExn(event));
|
38
|
+
const toBufferExn = (event: typeof schema.Type) =>
|
39
|
+
Buffer.from(toStringExn(event));
|
40
|
+
|
41
|
+
return {
|
42
|
+
decode,
|
43
|
+
decodeExn,
|
44
|
+
encode,
|
45
|
+
encodeExn,
|
46
|
+
fromString,
|
47
|
+
fromBuffer,
|
48
|
+
fromStringExn,
|
49
|
+
fromBufferExn,
|
50
|
+
toString,
|
51
|
+
toBuffer,
|
52
|
+
toStringExn,
|
53
|
+
toBufferExn
|
54
|
+
};
|
55
|
+
};
|