Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix all typescript compile errors #221

Merged
merged 4 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ lib/*

# test schema
schema.graphql

# webstorm
.idea
16 changes: 8 additions & 8 deletions src/engine/action/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type ActionSetup = {
output?: any;
resolve: ActionResolver;
type?: string;
permissions?: Function | Permission | Permission[];
permissions?: ((...args) => Permission | Permission[]) | Permission | Permission[];
// preProcessor?: Function;
// postProcessor?: Function;
preProcessor?: ActionPreProcessor;
Expand All @@ -70,7 +70,7 @@ export class Action {
resolve: ActionResolver;
type: string;
permissions: Permission | Permission[];
private _permissions: Function | Permission | Permission[];
private readonly _permissions: ((...args) => Permission | Permission[]) | Permission | Permission[];
private _defaultPermissions: Permission | Permission[];
descriptionPermissions: string | boolean;
preProcessor?: ActionPreProcessor;
Expand Down Expand Up @@ -152,7 +152,7 @@ export class Action {
}

if (isFunction(this.input)) {
const inputFn = this.input as Function;
const inputFn = this.input as (...args) => any;
this.input = inputFn();

passOrThrow(
Expand Down Expand Up @@ -198,7 +198,7 @@ export class Action {
}

if (isFunction(this.output)) {
const outputFn = this.output as Function;
const outputFn = this.output as (...args) => any;
this.output = outputFn();

passOrThrow(
Expand Down Expand Up @@ -237,8 +237,8 @@ export class Action {
_processPermissions(): null | Permission | Permission[] {
if (this._permissions) {
if (isFunction(this._permissions)) {
const permissionsFn = this._permissions as Function;
const permissions: Permission | Permission[] = permissionsFn();
const permissionsFn = this._permissions as (...args) => Permission | Permission[];
const permissions = permissionsFn();
return processActionPermissions(this, permissions);
}
const permissions = this._permissions as Permission | Permission[];
Expand All @@ -254,7 +254,7 @@ export class Action {
if (this.permissions) {
let permissions: Permission | Permission[];
if (isFunction(this._permissions)) {
const permissionsFn = this._permissions as Function;
const permissionsFn = this._permissions as (...args) => Permission | Permission[];
permissions = permissionsFn();
} else {
permissions = this._permissions as Permission | Permission[];
Expand All @@ -268,7 +268,7 @@ export class Action {
this._defaultPermissions = defaultPermissions;
}

getPermissions(): Permission | Permission[] {
getPermissions(): ((...args) => Permission | Permission[]) | Permission | Permission[] {
if ((!this._permissions && !this._defaultPermissions) || this.permissions) {
return this.permissions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/attribute/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export type AttributeBase = {
/**
* a custom mock data generator
*/
mock?: () => any;
mock?: (...args) => any;

/**
* attribute is an input for a mutation and not part of the storage model
Expand Down
4 changes: 2 additions & 2 deletions src/engine/datatype/ObjectDataType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ObjectDataType extends ComplexDataType {
attributeName: string,
): AttributeBase {
if (isFunction(rawAttribute.type)) {
const rawAttributeTypeFn = rawAttribute.type;
const rawAttributeTypeFn = rawAttribute.type as DataTypeFunction;
rawAttribute.type = rawAttributeTypeFn({
setup: {
name: attributeName,
Expand Down Expand Up @@ -151,7 +151,7 @@ export class ObjectDataType extends ComplexDataType {
return resultAttributes;
}

validate: DataTypeValidateType = ({ value }) => {
validate: DataTypeValidateType = ({ value }): void => {
if (value) {
if (!isMap(value)) {
throw new Error(`Object data type '${this.name}' expects an object`);
Expand Down
33 changes: 16 additions & 17 deletions src/engine/entity/ViewEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { Entity } from './Entity';

interface PreFilterType {
[key: string]: {
resolve: Function;
resolve: (...args) => any;
attributes: any;
};
}
Expand Down Expand Up @@ -84,21 +84,17 @@ export class ViewEntity {
postProcessor?: ViewEntityPostProcessor;
preFilters?: PreFilterType;
meta?: any;
private _attributesMap: AttributesSetupMap | AttributesMapGenerator;
private readonly _attributesMap: AttributesSetupMap | AttributesMapGenerator;
private _primaryAttribute: Attribute;
private referencedByEntities: {
sourceEntityName: string;
sourceAttributeName: string;
}[];
private _permissions: PermissionMap;
private _permissions: PermissionMap | ((...args) => any);
private _defaultPermissions: PermissionMap;
private _attributes: AttributesMap;
descriptionPermissionsFind: string | boolean;
descriptionPermissionsRead: string | boolean;
private _preFilters: PreFilterType | (() => PreFilterType);
isFallbackStorageType: any;
findOne: Function;
find: Function;
findOne: (...arg) => any;
find: (...args) => any;

constructor(setup: ViewEntitySetup) {
passOrThrow(isMap(setup), () => 'ViewEntity requires a setup object');
Expand Down Expand Up @@ -144,7 +140,6 @@ export class ViewEntity {
this.description = description;
this._attributesMap = attributes;
this._primaryAttribute = null;
this.referencedByEntities = [];
this.viewExpression = viewExpression;
this._permissions = permissions;
this._preFilters = preFilters;
Expand Down Expand Up @@ -183,7 +178,7 @@ export class ViewEntity {
}
}

_injectStorageTypeBySchema(storageType) {
_injectStorageTypeBySchema(storageType: StorageType): void {
passOrThrow(
isStorageType(storageType),
() => `Provided storage type to view entity '${this.name}' is invalid`,
Expand All @@ -195,12 +190,12 @@ export class ViewEntity {
}
}

_exposeStorageAccess() {
_exposeStorageAccess(): void {
this.findOne = this.storageType.findOne;
this.find = this.storageType.find;
}

_injectDefaultPermissionsBySchema(defaultPermissions) {
_injectDefaultPermissionsBySchema(defaultPermissions): void {
passOrThrow(
isMap(defaultPermissions),
() => 'Provided defaultPermissions is invalid',
Expand Down Expand Up @@ -426,11 +421,15 @@ export class ViewEntity {
return attributes[attributeName];
}

_processPermissions() {
_processPermissions(): PermissionMap | null {
if (this._permissions) {
const permissions = isFunction(this._permissions)
? this._permissions()
: this._permissions;
let permissions: PermissionMap;

if (typeof this._permissions === 'function') {
permissions = this._permissions();
} else {
permissions = this._permissions;
}

return processViewEntityPermissions(
this,
Expand Down
11 changes: 6 additions & 5 deletions src/engine/entity/systemAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import {

import { CustomError } from '../CustomError';
import { DataTypeState } from '../datatype/DataTypeState';
import { Entity, isEntity } from '../entity/Entity';
import { Entity, isEntity } from './Entity';
import { Mutation } from '../mutation/Mutation';
import { i18nMockGenerator } from '../i18n';
import { Attribute } from '../attribute/Attribute';
import { isViewEntity } from './ViewEntity';
import { Context } from '../context/Context';

export const systemAttributePrimary = {
Expand All @@ -34,7 +33,7 @@ export const systemAttributesTimeTracking: Attribute[] = [
defaultValue: ({ operation: mutation }: { operation: Mutation }): Date => {
return mutation.isTypeCreate ? new Date() : undefined;
},
mock: (entity: Entity) => {
mock: (entity: Entity): Date => {
if (entity.meta && entity.meta.mockCreatedAtGenerator) {
if (!isFunction(entity.meta.mockCreatedAtGenerator)) {
throw new CustomError(
Expand Down Expand Up @@ -117,7 +116,6 @@ export const systemAttributeState: Attribute = {
type: ({ setup: attribute, entity }) =>
new DataTypeState({
...attribute,
validate: undefined, // delete from props as it would be handled as a data type validator
name: camelCase(`${entity.name}-instance-state`),
states: isEntity(entity) ? entity.states : undefined,
}),
Expand All @@ -136,14 +134,17 @@ export const systemAttributeState: Attribute = {
},
serialize: (value, _: any, __: Mutation, entity: Entity) => {
const states = entity.getStates();
const state = states[value];
const state = states[value as any];

if (!state) {
throw new CustomError('State was not set', 'StateNotSetError');
}

return state;
},
// why ts-ignore? because mutation does not exist on the graphQl/Source class type as public member
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
validate: ({ value, source: { mutation } }) => {
if (mutation.isTypeCreate || mutation.isTypeUpdate) {
if (typeof mutation.toState !== 'string') {
Expand Down
18 changes: 13 additions & 5 deletions src/engine/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ export const fillDefaultValues = async (

const entityAttributes = entity.getAttributes();
const payloadAttributes = Object.keys(payload);
// TODO: isSystemAttribute does not exist? do we create it?
const requiredAttributes = _.filter(
entityAttributes,
(attribute) => attribute.required && !attribute.isSystemAttribute,
// eslint-disable-next-line dot-notation
(attribute) => attribute.required && !attribute['isSystemAttribute'],
);

await Promise.all(
requiredAttributes.map(async (attribute) => {
const attributeName = attribute.name;
// TODO: name does not exist on Attribute base
// eslint-disable-next-line dot-notation
const attributeName = attribute['name'];
if (!payloadAttributes.includes(attributeName)) {
if (attribute.defaultValue) {
ret[attributeName] = await attribute.defaultValue({
Expand All @@ -73,7 +77,7 @@ export const fillDefaultValues = async (
export const serializeValues = (
entity: Entity,
entityMutation: Mutation,
payload: any,
payload: Record<string, unknown>,
model: string,
context: Record<string, any>,
): any => {
Expand All @@ -84,10 +88,14 @@ export const serializeValues = (
const entityAttributes = entity.getAttributes();

_.forEach(entityAttributes, (attribute) => {
const attributeName = attribute.name;
// TODO: name does not exist on AttributeBase
// eslint-disable-next-line dot-notation
const attributeName = attribute['name'];
const { fieldNameI18n: gqlFieldNameI18n } = getRegisteredEntityAttribute(
entity.name,
attribute.name,
// TODO: name does not exist on AttributeBase
// eslint-disable-next-line dot-notation
attribute['name'],
);

if (attribute.serialize) {
Expand Down
8 changes: 4 additions & 4 deletions src/engine/permission/Permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ export type ActionPermissionFilter = {
};

export const buildActionPermissionFilter = async (
_permissions: () => Permission | Permission | Permission[],
_permissions: () => Permission | Permission[],
userId = null,
userRoles = [],
action: Action | Subscription,
Expand All @@ -840,10 +840,10 @@ export const buildActionPermissionFilter = async (
permissions = isArray(permissionResult)
? permissionResult
: [permissionResult];
} else if (isArray(_permissions as Permission[])) {
permissions = _permissions as Permission[];
} else if (isArray((_permissions as unknown) as Permission[])) {
permissions = _permissions as unknown as Permission[];
} else {
permissions = [_permissions] as Permission[];
permissions = ([_permissions] as unknown) as Permission[];
}

// const permissions = isArray(_permissions as Permission[])
Expand Down
20 changes: 10 additions & 10 deletions src/engine/storage/StorageConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class StorageConfiguration {

generateGetStateIdFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateGetStateIdFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -89,7 +89,7 @@ export class StorageConfiguration {

generateGetStateIdsFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateGetStateIdsFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -98,7 +98,7 @@ export class StorageConfiguration {

generateGetStateMapFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateGetStateMapFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -107,7 +107,7 @@ export class StorageConfiguration {

generateGetStateNameFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateGetStateNameFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -116,7 +116,7 @@ export class StorageConfiguration {

generateGetAttributeTranslationFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateGetAttributeTranslationFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -125,7 +125,7 @@ export class StorageConfiguration {

generateGetAttributeTranslationsFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateGetAttributeTranslationsFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -134,7 +134,7 @@ export class StorageConfiguration {

generateMergeTranslationsFunction(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`generateMergeTranslationsFunction() not implemented for storage type '${this.name}'`,
Expand All @@ -143,7 +143,7 @@ export class StorageConfiguration {

createI18nIndices(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
): string {
throw new Error(
`createI18nIndices() not implemented for storage type '${this.name}'`,
Expand All @@ -152,9 +152,9 @@ export class StorageConfiguration {

generateI18nIndicesMigration(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configuration: Configuration,
_configuration: Configuration,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
manager: unknown,
_manager: unknown,
): Promise<{ upQueries: string[]; downQueries: string[] }> {
throw new Error(
`generateI18nIndicesMigration() not implemented for storage type '${this.name}'`,
Expand Down