Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.

Commit 38697a9

Browse files
committed
Merge remote-tracking branch 'origin/normalizr3-typings'
Closes gh-189
2 parents bd5a7a4 + d596ba1 commit 38697a9

11 files changed

+224
-1
lines changed

index.d.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
declare namespace schema {
2+
export type SchemaFunction = (value: any, parent: any, key: string) => string
3+
export type MergeFunction = (entityA: any, entityB: any) => any
4+
5+
export class Array {
6+
constructor(definition: Schema, schemaAttribute?: string | SchemaFunction)
7+
define(definition: Schema): void
8+
}
9+
10+
export interface EntityOptions {
11+
idAttribute?: string | SchemaFunction
12+
mergeStrategy?: MergeFunction
13+
processStrategy?: SchemaFunction
14+
}
15+
16+
export class Entity {
17+
constructor(key: string, definition?: Schema, options?: EntityOptions)
18+
define(definition: Schema): void
19+
key(): string
20+
}
21+
22+
export class Object {
23+
constructor(definition: {[key: string]: Schema})
24+
define(definition: Schema): void
25+
}
26+
27+
export class Union {
28+
constructor(definition: Schema, schemaAttribute?: string | SchemaFunction)
29+
define(definition: Schema): void
30+
}
31+
32+
export class Values {
33+
constructor(definition: Schema, schemaAttribute?: string | SchemaFunction)
34+
define(definition: Schema): void
35+
}
36+
}
37+
38+
export type Schema =
39+
schema.Array |
40+
schema.Entity |
41+
schema.Object |
42+
schema.Union |
43+
schema.Values |
44+
schema.Array[] |
45+
schema.Entity[] |
46+
schema.Object[] |
47+
schema.Union[] |
48+
schema.Values[] |
49+
{[key: string]: Schema};
50+
51+
export function normalize(
52+
data: any,
53+
schema: Schema
54+
): {
55+
entities: any,
56+
result: any
57+
};

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
],
2020
"files": [
2121
"dist/",
22+
"index.d.ts",
2223
"LICENSE",
2324
"README.md"
2425
],
2526
"main": "dist/index.js",
27+
"typings": "index.d.ts",
2628
"scripts": {
2729
"build": "npm run clean && mkdirp dist && npm-run-all --parallel build:development build:production",
2830
"build:development": "NODE_ENV=development rollup -c",
@@ -56,6 +58,8 @@
5658
"rollup": "^0.37.0",
5759
"rollup-plugin-babel": "^2.7.1",
5860
"rollup-plugin-filesize": "^1.0.1",
59-
"rollup-plugin-uglify": "^1.0.1"
61+
"rollup-plugin-uglify": "^1.0.1",
62+
"typescript": "^2.1.4",
63+
"typescript-definition-tester": "0.0.5"
6064
}
6165
}

src/__tests__/typescript.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-env jest */
2+
import * as path from 'path';
3+
import { compileDirectory } from 'typescript-definition-tester';
4+
5+
describe('TypeScript definitions', () => {
6+
it('compile against index.d.ts', (done) => {
7+
compileDirectory(
8+
path.resolve(__dirname, 'typescript'),
9+
(fileName) => fileName.match(/\.ts$/),
10+
done
11+
);
12+
});
13+
});

src/__tests__/typescript/array.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const data = [ { id: '123', name: 'Jim' }, { id: '456', name: 'Jane' } ];
4+
const userSchema = new schema.Entity('users');
5+
6+
const userListSchema = new schema.Array(userSchema);
7+
const normalizedData = normalize(data, userListSchema);
8+
9+
const userListSchemaAlt = [ userSchema ];
10+
const normalizedDataAlt = normalize(data, userListSchemaAlt);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const data = [ { id: 1, type: 'admin' }, { id: 2, type: 'user' } ];
4+
const userSchema = new schema.Entity('users');
5+
const adminSchema = new schema.Entity('admins');
6+
7+
const myArray = new schema.Array({
8+
admins: adminSchema,
9+
users: userSchema
10+
}, (input, parent, key) => `${input.type}s`);
11+
12+
const normalizedData = normalize(data, myArray);

src/__tests__/typescript/entity.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const data = {/*...*/};
4+
const user = new schema.Entity('users', {}, { idAttribute: 'id_str' });
5+
const tweet = new schema.Entity('tweets', { user: user }, {
6+
idAttribute: 'id_str',
7+
// Apply everything from entityB over entityA, except for "favorites"
8+
mergeStrategy: (entityA, entityB) => ({
9+
...entityA,
10+
...entityB,
11+
favorites: entityA.favorites
12+
}),
13+
// Remove the URL field from the entity
14+
processStrategy: (entity) => {
15+
const {url, ...entityWithoutUrl} = entity;
16+
return entityWithoutUrl;
17+
}
18+
});
19+
20+
const normalizedData = normalize(data, tweet);

src/__tests__/typescript/github.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const user = new schema.Entity('users');
4+
5+
const label = new schema.Entity('labels');
6+
7+
const milestone = new schema.Entity('milestones', {
8+
creator: user
9+
});
10+
11+
const issue = new schema.Entity('issues', {
12+
assignee: user,
13+
assignees: [ user ],
14+
labels: label,
15+
milestone,
16+
user
17+
});
18+
19+
const pullRequest = new schema.Entity('pullRequests', {
20+
assignee: user,
21+
assignees: [ user ],
22+
labels: label,
23+
milestone,
24+
user
25+
});
26+
27+
const issueOrPullRequest = new schema.Array({
28+
issues: issue,
29+
pullRequests: pullRequest
30+
}, (entity: any) => entity.pull_request ? 'pullRequests' : 'issues');
31+
32+
const data = {/*...*/};
33+
const normalizedData = normalize(data, issueOrPullRequest);
34+
console.log(normalizedData);

src/__tests__/typescript/object.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const data = {/*...*/};
4+
const user = new schema.Entity('users');
5+
6+
const responseSchema = new schema.Object({ users: new schema.Array(user) });
7+
const normalizedData = normalize(data, responseSchema);
8+
9+
const responseSchemaAlt = { users: new schema.Array(user) };
10+
const normalizedDataAlt = normalize(data, responseSchemaAlt);
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const userProcessStrategy = (value: any, parent: any, key: string) => {
4+
switch (key) {
5+
case 'author':
6+
return { ...value, posts: [ parent.id ] };
7+
case 'commenter':
8+
return { ...value, comments: [ parent.id ] };
9+
default:
10+
return { ...value };
11+
}
12+
};
13+
14+
const userMergeStrategy = (entityA: any, entityB: any) => {
15+
return {
16+
...entityA,
17+
...entityB,
18+
posts: [ ...(entityA.posts || []), ...(entityB.posts || []) ],
19+
comments: [ ...(entityA.comments || []), ...(entityB.comments || []) ]
20+
};
21+
};
22+
23+
const user = new schema.Entity('users', {}, {
24+
mergeStrategy: userMergeStrategy,
25+
processStrategy: userProcessStrategy
26+
});
27+
28+
const comment = new schema.Entity('comments', {
29+
commenter: user
30+
}, {
31+
processStrategy: (value: any, parent: any, key: string) => {
32+
return { ...value, post: parent.id };
33+
}
34+
});
35+
36+
const post = new schema.Entity('posts', {
37+
author: user,
38+
comments: [ comment ]
39+
});
40+
41+
const data = {/*...*/};
42+
const normalizedData = normalize(data, post);
43+
console.log(normalizedData);

src/__tests__/typescript/union.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const data = { owner: { id: 1, type: 'user' } };
4+
5+
const user = new schema.Entity('users');
6+
const group = new schema.Entity('groups');
7+
const unionSchema = new schema.Union({
8+
user: user,
9+
group: group
10+
}, 'type');
11+
12+
const normalizedData = normalize(data, { owner: unionSchema });

src/__tests__/typescript/values.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { normalize, schema } from '../../../index';
2+
3+
const data = { firstThing: { id: 1 }, secondThing: { id: 2 } };
4+
5+
const item = new schema.Entity('items');
6+
const valuesSchema = new schema.Values(item);
7+
8+
const normalizedData = normalize(data, valuesSchema);

0 commit comments

Comments
 (0)