Skip to content

Commit

Permalink
feat(core/transform): doesn't change type names on normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 16, 2019
1 parent 5bde885 commit 3bc8d64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
25 changes: 10 additions & 15 deletions packages/core/src/transform/normalize/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function normalizeServiceTypes(
for (const kind of ['request', 'response'] as ['request', 'response']) {
const type = service.types[kind];
if (typeof type === 'string') {
service.types[kind] = checkSourceType(kind, type, skip, types, transform);
service.types[kind] = checkSourceType(kind, type, skip, types);
} else {
const pascal = name + transform('R' + kind.slice(1), false);
normalizeServiceType(kind, pascal, type, skip, types, transform);
Expand Down Expand Up @@ -64,7 +64,7 @@ export function normalizeErrors(
const result: ServiceErrors = {};
for (const [key, error] of Object.entries(errors)) {
if (typeof error === 'string') {
let id = checkSourceType('error', error, skip, types, transform);
let id = checkSourceType('error', error, skip, types);
if (key !== error) {
id = transform(key, true);
normalizeServiceType(
Expand Down Expand Up @@ -155,24 +155,19 @@ export function checkSourceType(
kind: string,
name: string,
skip: boolean | string[],
types: { source: TreeTypes; normal: TreeTypes },
transform: (str: string, isExplicit: boolean) => string
types: { source: TreeTypes; normal: TreeTypes }
): string {
const pascal = transform(name, true);

if (skip && (typeof skip === 'boolean' || skip.includes(name))) {
return pascal;
return name;
}

if (
!Object.hasOwnProperty.call(types.source, name) ||
!Object.hasOwnProperty.call(types.normal, pascal)
) {
if (!Object.hasOwnProperty.call(types.normal, name)) {
throw Error(`Collection lacks referenced type: ${name}`);
}
if (types.normal[pascal].kind !== kind) {
throw Error(`Invalid type kind reference: ${pascal}`);
if (types.normal[name].kind !== kind) {
throw Error(
`Invalid type kind reference -expected "${kind}" but got "${types.normal[name].kind}": ${name}`
);
}

return pascal;
return name;
}
22 changes: 9 additions & 13 deletions packages/core/src/transform/normalize/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
CollectionTree,
TreeTypes,
QueryService,
SubscriptionService,
NormalCollection
Expand Down Expand Up @@ -28,7 +27,7 @@ export interface NormalizeTransformOptions {
* - Produces conflicting type names.
* - Contains references to non existent types.
* - Has a scope name equal to a service of its parent.
* - Has a type name equal to a collection root service name.
* - Has a type name starting with a lowercase letter or equal to a collection root service name.
* - Contains types, services, or scopes with an empty name or with non word characters.
* - Contains services with inline types or type references of the wrong kind.
*/
Expand All @@ -44,19 +43,16 @@ export function normalize<T extends CollectionTree>(

const types = {
source: collection.types,
normal: Object.entries(collection.types).reduce(
(acc: TreeTypes, [name, type]) => {
const pascal = transform(name, true);
if (Object.hasOwnProperty.call(acc, pascal)) {
throw Error(`Type name collision: ${pascal}`);
}
acc[pascal] = type;
return acc;
},
{}
)
normal: { ...collection.types }
};

const lowercase = Object.keys(collection.types).filter(
(x) => x[0] && x[0] !== x[0].toUpperCase()
);
if (lowercase.length) {
throw Error(`Types must start with an uppercase letter`);
}

const result = {
...replace(collection, (element, next, { route }) => {
if (isElementTree(element) && isTreeCollection(element)) {
Expand Down

0 comments on commit 3bc8d64

Please sign in to comment.