Skip to content

Commit

Permalink
feat(core/transform): normalize also takes intercepts into account
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 16, 2019
1 parent d0a547a commit 5a4f525
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 23 deletions.
44 changes: 31 additions & 13 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@riseup/tooling": "^0.4.0",
"@types/jest": "^24.0.13",
"@types/lodash.clonedeep": "^4.5.6",
"@types/lodash.isequal": "^4.5.5",
"@zerollup/ts-transform-paths": "^1.7.3",
"kpo": "^0.11.0",
"onchange": "^6.0.0",
Expand All @@ -80,7 +81,8 @@
"@types/json-schema": "^7.0.3",
"camelcase": "^5.3.1",
"json-schema-to-typescript": "^7.1.0",
"lodash.clonedeep": "^4.5.0"
"lodash.clonedeep": "^4.5.0",
"lodash.isequal": "^4.5.0"
},
"peerDependencies": {
"rxjs": "6.x"
Expand Down
60 changes: 51 additions & 9 deletions packages/core/src/transform/normalize/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import {
ServiceErrors,
Type,
QueryService,
SubscriptionService
SubscriptionService,
InterceptImplementation
} from '~/types';
import { isServiceImplementation } from '~/inspect';
import isequal from 'lodash.isequal';

export function normalizeServiceTypes(
name: string,
Expand All @@ -26,8 +29,37 @@ export function normalizeServiceTypes(
}
}

const errors: ServiceErrors = {};
for (const [key, error] of Object.entries(service.types.errors)) {
service.types.errors = normalizeErrors(
service.types.errors,
types,
transform
);

if (
isServiceImplementation(service) &&
service.intercepts &&
service.intercepts.length
) {
const intercepts: InterceptImplementation[] = [];
for (const intercept of service.intercepts) {
intercepts.push({
...intercept,
errors: normalizeErrors(intercept.errors, types, transform)
});
}
service.intercepts = intercepts;
}

return service;
}

export function normalizeErrors(
errors: ServiceErrors,
types: { source: TreeTypes; normal: TreeTypes },
transform: (str: string, isExplicit: boolean) => string
): ServiceErrors {
const result: ServiceErrors = {};
for (const [key, error] of Object.entries(errors)) {
if (typeof error === 'string') {
let id = checkSourceType('error', error, types, transform);
if (key !== error) {
Expand All @@ -40,16 +72,14 @@ export function normalizeServiceTypes(
transform
);
}
errors[id] = id;
result[id] = id;
} else {
const id = transform(key, true);
normalizeServiceType('error', id, error, types, transform);
errors[id] = id;
result[id] = id;
}
}
service.types.errors = errors;

return service;
return result;
}

export function normalizeServiceType(
Expand All @@ -64,7 +94,19 @@ export function normalizeServiceType(
}

switch (type.kind) {
case 'error':
case 'error': {
// In the case of errors we'll check for deep equality.
// This is specially important for intercepts, which might
// make inline declarations repeat themselves.
if (
Object.hasOwnProperty.call(types.normal, name) &&
!isequal(types.normal[name], type)
) {
throw Error(`Inline type name collision: ${name}`);
}
types.normal[name] = type;
return;
}
case 'request': {
if (Object.hasOwnProperty.call(types.normal, name)) {
throw Error(`Inline type name collision: ${name}`);
Expand Down

0 comments on commit 5a4f525

Please sign in to comment.