From 1aeccd7ab67603e33b2ab07abcd3825b8ab71980 Mon Sep 17 00:00:00 2001 From: Serhii Matrunchyk Date: Tue, 26 May 2020 01:21:53 +0300 Subject: [PATCH] fix(repository): fix model instantiation in fromArray call --- package.json | 2 +- src/models/Model.ts | 22 ++++++++++------------ src/repositories/Repository.ts | 4 ++-- src/utils.ts | 19 ++++--------------- tsconfig.json | 5 ----- tsconfig.tsbuildinfo | 15 +++++---------- 6 files changed, 22 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index 175b041..02aec1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-oop", - "version": "1.1.1", + "version": "1.1.2", "description": "A library based on Model-Repository patterns for Vue components. Usable for GraphQL and REST APIs.", "keywords": [ "collections", diff --git a/src/models/Model.ts b/src/models/Model.ts index 1e9fba2..0232f28 100644 --- a/src/models/Model.ts +++ b/src/models/Model.ts @@ -21,13 +21,11 @@ export default abstract class Model extends EventEmitter { public loading = false; - public children: unknown; + public createMutation?: string | CallableFunction | DocumentNode; - public createMutation?: string | Function | DocumentNode; + public updateMutation?: string | CallableFunction | DocumentNode; - public updateMutation?: string | Function | DocumentNode; - - public deleteMutation?: string | Function | DocumentNode; + public deleteMutation?: string | CallableFunction | DocumentNode; protected submittableProps = []; @@ -213,7 +211,7 @@ export default abstract class Model extends EventEmitter { * @param {string} method * @returns {*|Promise>|void} */ - public async mutate(mutationOrUrl, params, method = this.defaultMethod) { + public async mutate(mutationOrUrl, params, method = this.defaultMethod): Promise { // istanbul ignore else if (config().graphql) { return this.beforeMutate() @@ -231,35 +229,35 @@ export default abstract class Model extends EventEmitter { .finally(this.markExists.bind(this)); } - public clone() { + public clone(): this { return clone(this).markNotExists(); } - public markExists() { + public markExists(): this { this._exists = true; return this; } - public markNotExists() { + public markNotExists(): this { this._exists = false; return this; } - protected async beforeMutate() { + protected async beforeMutate(): Promise { this.loading = true; return this; } - protected afterMutate() { + protected afterMutate(): this { this.loading = false; return this; } - protected resolveRequest(request: string | DocumentNode, defaultRequest: string | Function | DocumentNode): string | DocumentNode { + protected resolveRequest(request: string | DocumentNode, defaultRequest: string | CallableFunction | DocumentNode): string | DocumentNode { if (request) return request; return typeof defaultRequest === 'function' ? defaultRequest() : defaultRequest; diff --git a/src/repositories/Repository.ts b/src/repositories/Repository.ts index ae4e998..04dc6df 100644 --- a/src/repositories/Repository.ts +++ b/src/repositories/Repository.ts @@ -2,7 +2,7 @@ import { GraphQLError, DocumentNode } from 'graphql'; import InvalidArgumentException from '../models/Exceptions/InvalidArgumentException'; import Collection from '../models/Collection'; // noinspection ES6PreferShortImport -import { getUrl, config, performSafeRequestREST, performSafeRequestGraphql } from '../utils'; +import { getUrl, config, performSafeRequestREST, performSafeRequestGraphql, isClass } from '../utils'; import UnexpectedException from '../models/Exceptions/UnexpectedException'; import ValidationException from '../models/Exceptions/ValidationException'; import UnauthorizedException from '../models/Exceptions/UnauthorizedException'; @@ -136,7 +136,7 @@ export default abstract class Repository extends EventEmitter { const mapped = filtered.map((i) => { let model = this.model; - if (typeof this.model === 'function') { + if (typeof this.model === 'function' && !isClass(this.model as CallableFunction)) { model = this.model(i); } diff --git a/src/utils.ts b/src/utils.ts index ad7ed60..5a2d0b2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -121,24 +121,11 @@ export async function performSafeRequestGraphql(query: DocumentNode, variables = const isQuery = (query.definitions.find(def => def.kind === 'OperationDefinition')).operation === 'query'; if (isQuery) { return performGqlQuery(query, stripTypename(variables)) - .then((value: ApolloQueryResult) => { - console.log(value); - return value.data[queryName]; - }); + .then((value: ApolloQueryResult) => value.data[queryName]); } return performGqlMutation(query, stripTypename(variables)) - .then((value: FetchResult) => { - console.log(value); - return value.data[queryName]; - }); - - // @ts-ignore - return operation().then((result: ApolloQueryResult) => { - console.log(result); - - return result.data[queryName]; - }); + .then((value: FetchResult) => value.data[queryName]); } export function registryGet(key: string): unknown { @@ -224,3 +211,5 @@ export function fetchIntrospectionSchema(url: string): Promise res.json()) .then(res => res.data); } + +export const isClass = (fn: CallableFunction): boolean => /^\s*class/.test(fn.toString()); diff --git a/tsconfig.json b/tsconfig.json index 5ba7906..1903342 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -66,11 +66,6 @@ "baseUrl": ".", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ - "paths": { - "@/*": [ - "src/*" - ] - }, // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ "types": [ diff --git a/tsconfig.tsbuildinfo b/tsconfig.tsbuildinfo index 498d879..f1b9e68 100644 --- a/tsconfig.tsbuildinfo +++ b/tsconfig.tsbuildinfo @@ -1257,8 +1257,8 @@ "affectsGlobalScope": false }, "./src/utils.ts": { - "version": "fef6b0a4c07b64c48df3ff3b01dd485d0e66db738f51ef04d3684d014bbd7cd5", - "signature": "6f3e13a2842e761f331abd036ba2fa7ae2ae6bd7b150a6c8941d3732acb81954", + "version": "f74ca835e4aeb527ae03d4f5a022b2623fbeb92ecf0a5a7bd6baeeb1d3d242b9", + "signature": "7a028338d53439831635555afca4d373ee6c5fd071784267dd4087e29890eaf9", "affectsGlobalScope": false }, "./src/models/exceptions/invalidargumentexception.ts": { @@ -1352,8 +1352,8 @@ "affectsGlobalScope": false }, "./src/models/model.ts": { - "version": "f3fda724890ec3ed31d3591f7c02a319b6c539162301bb7a2144005b410b2f5a", - "signature": "6a740806560eac75ddfc8df904abcd5a9bb7f3088d336c7256b94cf1eb0a3ba0", + "version": "e32b59d99b901e40552904d650adbc9d6669f12ef4c2c57fedf9813b6e465cf2", + "signature": "564b8ef0af6ce279ab347f189ba78494e8fadbde55de9cdc4e609fb5e9932e21", "affectsGlobalScope": false }, "./src/models/collection.ts": { @@ -1372,7 +1372,7 @@ "affectsGlobalScope": false }, "./src/repositories/repository.ts": { - "version": "0de2903719241cdde3b42dbf13f5358b090b3c8bfae054e22ef6879de36fec7a", + "version": "39753a93888396628e05b12cde3c0014cce846bf4fda0e98a436a541c0956a69", "signature": "fe983ab1b350f3104bb0db62fb820f32f478834e52492118bf1d73024dec0077", "affectsGlobalScope": false }, @@ -1763,11 +1763,6 @@ "noFallthroughCasesInSwitch": true, "moduleResolution": 2, "baseUrl": "./", - "paths": { - "@/*": [ - "src/*" - ] - }, "types": [ "node", "jest"