Skip to content

Commit

Permalink
fix(repository): fix model instantiation in fromArray call
Browse files Browse the repository at this point in the history
  • Loading branch information
matrunchyk committed May 25, 2020
1 parent 8df9ec1 commit 1aeccd7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 10 additions & 12 deletions src/models/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -213,7 +211,7 @@ export default abstract class Model extends EventEmitter {
* @param {string} method
* @returns {*|Promise<Array<any>>|void}
*/
public async mutate(mutationOrUrl, params, method = this.defaultMethod) {
public async mutate(mutationOrUrl, params, method = this.defaultMethod): Promise<unknown> {
// istanbul ignore else
if (config().graphql) {
return this.beforeMutate()
Expand All @@ -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> {
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;
Expand Down
4 changes: 2 additions & 2 deletions src/repositories/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -136,7 +136,7 @@ export default abstract class Repository<M = unknown> 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);
}

Expand Down
19 changes: 4 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,11 @@ export async function performSafeRequestGraphql(query: DocumentNode, variables =
const isQuery = (<OperationDefinitionNode>query.definitions.find(def => def.kind === 'OperationDefinition')).operation === 'query';
if (isQuery) {
return performGqlQuery(query, stripTypename(variables))
.then((value: ApolloQueryResult<unknown>) => {
console.log(value);
return value.data[queryName];
});
.then((value: ApolloQueryResult<unknown>) => value.data[queryName]);
}

return performGqlMutation(query, stripTypename(variables))
.then((value: FetchResult<unknown>) => {
console.log(value);
return value.data[queryName];
});

// @ts-ignore
return operation().then((result: ApolloQueryResult<T>) => {
console.log(result);

return result.data[queryName];
});
.then((value: FetchResult<unknown>) => value.data[queryName]);
}

export function registryGet(key: string): unknown {
Expand Down Expand Up @@ -224,3 +211,5 @@ export function fetchIntrospectionSchema(url: string): Promise<IntrospectionQuer
.then(res => res.json())
.then(res => res.data);
}

export const isClass = (fn: CallableFunction): boolean => /^\s*class/.test(fn.toString());
5 changes: 0 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
15 changes: 5 additions & 10 deletions tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
Expand Up @@ -1257,8 +1257,8 @@
"affectsGlobalScope": false
},
"./src/utils.ts": {
"version": "fef6b0a4c07b64c48df3ff3b01dd485d0e66db738f51ef04d3684d014bbd7cd5",
"signature": "6f3e13a2842e761f331abd036ba2fa7ae2ae6bd7b150a6c8941d3732acb81954",
"version": "f74ca835e4aeb527ae03d4f5a022b2623fbeb92ecf0a5a7bd6baeeb1d3d242b9",
"signature": "7a028338d53439831635555afca4d373ee6c5fd071784267dd4087e29890eaf9",
"affectsGlobalScope": false
},
"./src/models/exceptions/invalidargumentexception.ts": {
Expand Down Expand Up @@ -1352,8 +1352,8 @@
"affectsGlobalScope": false
},
"./src/models/model.ts": {
"version": "f3fda724890ec3ed31d3591f7c02a319b6c539162301bb7a2144005b410b2f5a",
"signature": "6a740806560eac75ddfc8df904abcd5a9bb7f3088d336c7256b94cf1eb0a3ba0",
"version": "e32b59d99b901e40552904d650adbc9d6669f12ef4c2c57fedf9813b6e465cf2",
"signature": "564b8ef0af6ce279ab347f189ba78494e8fadbde55de9cdc4e609fb5e9932e21",
"affectsGlobalScope": false
},
"./src/models/collection.ts": {
Expand All @@ -1372,7 +1372,7 @@
"affectsGlobalScope": false
},
"./src/repositories/repository.ts": {
"version": "0de2903719241cdde3b42dbf13f5358b090b3c8bfae054e22ef6879de36fec7a",
"version": "39753a93888396628e05b12cde3c0014cce846bf4fda0e98a436a541c0956a69",
"signature": "fe983ab1b350f3104bb0db62fb820f32f478834e52492118bf1d73024dec0077",
"affectsGlobalScope": false
},
Expand Down Expand Up @@ -1763,11 +1763,6 @@
"noFallthroughCasesInSwitch": true,
"moduleResolution": 2,
"baseUrl": "./",
"paths": {
"@/*": [
"src/*"
]
},
"types": [
"node",
"jest"
Expand Down

0 comments on commit 1aeccd7

Please sign in to comment.