diff --git a/.eslintrc b/.eslintrc index 00c4a19..a811bb2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -7,7 +7,8 @@ "extends": [ "eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended" + "plugin:@typescript-eslint/recommended", + "prettier" ], "rules": { "@typescript-eslint/no-inferrable-types": "off" diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..491fc35 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +node_modules +lib diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..6253224 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "singleQuote": true +} diff --git a/package-lock.json b/package-lock.json index cc37099..c387d7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,9 +19,11 @@ "@typescript-eslint/eslint-plugin": "^4.19.0", "@typescript-eslint/parser": "^4.19.0", "eslint": "^7.22.0", + "eslint-config-prettier": "^8.1.0", "jest": "^26.6.3", "lodash.camelcase": "^4.3.0", "miragejs": "^0.1.41", + "prettier": "2.2.1", "rimraf": "^2.6.2", "ts-jest": "^26.5", "typescript": "^4.2" @@ -2440,6 +2442,18 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/eslint-config-prettier": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.1.0.tgz", + "integrity": "sha512-oKMhGv3ihGbCIimCAjqkdzx2Q+jthoqnXSP+d86M9tptwugycmTFdVR4IpLgq2c4SHifbwO90z2fQ8/Aio73yw==", + "dev": true, + "bin": { + "eslint-config-prettier": "bin/cli.js" + }, + "peerDependencies": { + "eslint": ">=7.0.0" + } + }, "node_modules/eslint-scope": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz", @@ -5563,6 +5577,18 @@ "route-recognizer": "^0.3.3" } }, + "node_modules/prettier": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz", + "integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/pretty-format": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-26.6.2.tgz", @@ -9727,6 +9753,13 @@ } } }, + "eslint-config-prettier": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.1.0.tgz", + "integrity": "sha512-oKMhGv3ihGbCIimCAjqkdzx2Q+jthoqnXSP+d86M9tptwugycmTFdVR4IpLgq2c4SHifbwO90z2fQ8/Aio73yw==", + "dev": true, + "requires": {} + }, "eslint-scope": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz", @@ -12108,6 +12141,12 @@ "route-recognizer": "^0.3.3" } }, + "prettier": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz", + "integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==", + "dev": true + }, "pretty-format": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-26.6.2.tgz", diff --git a/package.json b/package.json index a9889ea..affd3c9 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,11 @@ "@typescript-eslint/eslint-plugin": "^4.19.0", "@typescript-eslint/parser": "^4.19.0", "eslint": "^7.22.0", + "eslint-config-prettier": "^8.1.0", "jest": "^26.6.3", "lodash.camelcase": "^4.3.0", "miragejs": "^0.1.41", + "prettier": "2.2.1", "rimraf": "^2.6.2", "ts-jest": "^26.5", "typescript": "^4.2" diff --git a/src/builders/urlBuilder.ts b/src/builders/urlBuilder.ts index 9651d32..02077db 100644 --- a/src/builders/urlBuilder.ts +++ b/src/builders/urlBuilder.ts @@ -1,18 +1,19 @@ -import {Model} from '../model'; -import {Orion} from '../orion'; -import {ModelConstructor} from '../contracts/modelConstructor'; -import {ExtractModelAttributesType} from '../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../types/extractModelRelationsType'; -import {ExtractModelKeyType} from "../types/extractModelKeyType"; +import { Model } from '../model'; +import { Orion } from '../orion'; +import { ModelConstructor } from '../contracts/modelConstructor'; +import { ExtractModelAttributesType } from '../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../types/extractModelRelationsType'; +import { ExtractModelKeyType } from '../types/extractModelKeyType'; export class UrlBuilder { - public static getResourceBaseUrl, PersistedAttributes = ExtractModelPersistedAttributesType, Relations = ExtractModelRelationsType, Key = ExtractModelKeyType - >(model: ModelConstructor): string { + >(model: ModelConstructor): string { return Orion.getApiUrl() + '/' + (model.prototype as M).$getResourceName(); } @@ -23,12 +24,13 @@ export class UrlBuilder { ); } - public static getRelationResourceUrl, PersistedAttributes = ExtractModelPersistedAttributesType, Relations = ExtractModelRelationsType, Key = ExtractModelKeyType - >( + >( parent: Model, relationConstructor: ModelConstructor ): string { diff --git a/src/contracts/modelConstructor.ts b/src/contracts/modelConstructor.ts index 798bc8d..c85a872 100644 --- a/src/contracts/modelConstructor.ts +++ b/src/contracts/modelConstructor.ts @@ -1,15 +1,16 @@ -import {Model} from '../model'; -import {ExtractModelAttributesType} from '../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../types/extractModelRelationsType'; -import {ExtractModelKeyType} from "../types/extractModelKeyType"; +import { Model } from '../model'; +import { ExtractModelAttributesType } from '../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../types/extractModelRelationsType'; +import { ExtractModelKeyType } from '../types/extractModelKeyType'; -export interface ModelConstructor, PersistedAttributes = ExtractModelPersistedAttributesType, Relations = ExtractModelRelationsType, Key = ExtractModelKeyType, - AllAttributes = Attributes & PersistedAttributes, - > { - new(attributes?: AllAttributes, relations?: Relations): M; + AllAttributes = Attributes & PersistedAttributes +> { + new (attributes?: AllAttributes, relations?: Relations): M; } diff --git a/src/drivers/default/builders/queryBuilder.ts b/src/drivers/default/builders/queryBuilder.ts index eb23744..77b974d 100644 --- a/src/drivers/default/builders/queryBuilder.ts +++ b/src/drivers/default/builders/queryBuilder.ts @@ -1,28 +1,29 @@ -import {HttpMethod} from '../enums/httpMethod'; -import {Model} from '../../../model'; -import {ModelConstructor} from '../../../contracts/modelConstructor'; -import {Scope} from '../scope'; -import {Filter} from '../filter'; -import {FilterOperator} from '../enums/filterOperator'; -import {FilterType} from '../enums/filterType'; -import {Sorter} from '../sorter'; -import {SortDirection} from '../enums/sortDirection'; -import {UrlBuilder} from '../../../builders/urlBuilder'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; -import {HttpClient} from '../../../httpClient'; -import {AxiosResponse} from 'axios'; -import {Orion} from '../../../orion'; -import {ExtractModelKeyType} from "../../../types/extractModelKeyType"; - -export class QueryBuilder, PersistedAttributes = ExtractModelPersistedAttributesType, Relations = ExtractModelRelationsType, Key = ExtractModelKeyType, - AllAttributes = Attributes & PersistedAttributes, - > { + AllAttributes = Attributes & PersistedAttributes +> { protected baseUrl: string; protected modelConstructor: ModelConstructor; protected httpClient: HttpClient; @@ -54,7 +55,7 @@ export class QueryBuilder { @@ -66,12 +67,12 @@ export class QueryBuilder, PersistedAttributes = ExtractModelPersistedAttributesType, Relations = ExtractModelRelationsType, Key = ExtractModelKeyType - > extends QueryBuilder { +> extends QueryBuilder { constructor( - relationConstructor: ModelConstructor, + relationConstructor: ModelConstructor< + Relation, + Attributes, + PersistedAttributes, + Relations, + Key + >, parent: Model ) { super(relationConstructor); diff --git a/src/drivers/default/enums/authDriver.ts b/src/drivers/default/enums/authDriver.ts index ba78877..9ee6359 100644 --- a/src/drivers/default/enums/authDriver.ts +++ b/src/drivers/default/enums/authDriver.ts @@ -1,5 +1,5 @@ export enum AuthDriver { Default = 'default', Passport = 'passport', - Sanctum = 'sanctum' + Sanctum = 'sanctum', } diff --git a/src/drivers/default/enums/filterOperator.ts b/src/drivers/default/enums/filterOperator.ts index ab09214..547a058 100644 --- a/src/drivers/default/enums/filterOperator.ts +++ b/src/drivers/default/enums/filterOperator.ts @@ -8,5 +8,5 @@ export enum FilterOperator { Like = 'like', NotLike = 'not like', In = 'in', - NotIn = 'not in' + NotIn = 'not in', } diff --git a/src/drivers/default/enums/filterType.ts b/src/drivers/default/enums/filterType.ts index 9eb6ac3..9a05fac 100644 --- a/src/drivers/default/enums/filterType.ts +++ b/src/drivers/default/enums/filterType.ts @@ -1,4 +1,4 @@ export enum FilterType { And = 'and', - Or = 'or' + Or = 'or', } diff --git a/src/drivers/default/enums/httpMethod.ts b/src/drivers/default/enums/httpMethod.ts index 118b9ba..c85391f 100644 --- a/src/drivers/default/enums/httpMethod.ts +++ b/src/drivers/default/enums/httpMethod.ts @@ -3,5 +3,5 @@ export enum HttpMethod { POST = 'post', PATCH = 'patch', PUT = 'put', - DELETE = 'delete' + DELETE = 'delete', } diff --git a/src/drivers/default/enums/sortDirection.ts b/src/drivers/default/enums/sortDirection.ts index 2db3b56..e42dd9d 100644 --- a/src/drivers/default/enums/sortDirection.ts +++ b/src/drivers/default/enums/sortDirection.ts @@ -1,4 +1,4 @@ export enum SortDirection { Asc = 'asc', - Desc = 'desc' + Desc = 'desc', } diff --git a/src/drivers/default/filter.ts b/src/drivers/default/filter.ts index 18baeaf..379ff33 100644 --- a/src/drivers/default/filter.ts +++ b/src/drivers/default/filter.ts @@ -1,5 +1,5 @@ -import {FilterOperator} from './enums/filterOperator'; -import {FilterType} from './enums/filterType'; +import { FilterOperator } from './enums/filterOperator'; +import { FilterType } from './enums/filterType'; export class Filter { constructor( @@ -7,8 +7,7 @@ export class Filter { protected operator: FilterOperator, protected value: any, protected type?: FilterType - ) { - } + ) {} public getField(): string { return this.field; diff --git a/src/drivers/default/relations/belongsTo.ts b/src/drivers/default/relations/belongsTo.ts index b0d2428..34f17a5 100644 --- a/src/drivers/default/relations/belongsTo.ts +++ b/src/drivers/default/relations/belongsTo.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { RelationQueryBuilder } from '../builders/relationQueryBuilder'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class BelongsTo, PersistedAttributes = ExtractModelPersistedAttributesType, Relations = ExtractModelRelationsType - > extends RelationQueryBuilder { -} +> extends RelationQueryBuilder {} diff --git a/src/drivers/default/relations/belongsToMany.ts b/src/drivers/default/relations/belongsToMany.ts index a92c968..ccedd87 100644 --- a/src/drivers/default/relations/belongsToMany.ts +++ b/src/drivers/default/relations/belongsToMany.ts @@ -1,20 +1,22 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {HttpMethod} from '../enums/httpMethod'; -import {AttachResult} from '../results/attachResult'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {DetachResult} from '../results/detachResult'; -import {SyncResult} from '../results/syncResult'; -import {ToggleResult} from '../results/toggleResult'; -import {UpdatePivotResult} from '../results/updatePivotResult'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; - -export class BelongsToMany, Attributes = ExtractModelAttributesType, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType> extends RelationQueryBuilder { + Relations = ExtractModelRelationsType +> extends RelationQueryBuilder { public async attach( keys: Array, duplicates: boolean = false @@ -22,9 +24,9 @@ export class BelongsToMany): Promise { const response = await this.httpClient.request(`/detach`, HttpMethod.DELETE, null, { - resources: keys + resources: keys, }); return new DetachResult(response.data.detached); @@ -55,7 +57,7 @@ export class BelongsToMany): Promise { const response = await this.httpClient.request(`/detach`, HttpMethod.DELETE, null, { - resources + resources, }); return new DetachResult(response.data.detached); @@ -65,9 +67,9 @@ export class BelongsToMany): Promise { const response = await this.httpClient.request(`/toggle`, HttpMethod.PATCH, null, { - resources: keys + resources: keys, }); return new ToggleResult(response.data.attached, response.data.detached); @@ -98,7 +100,7 @@ export class BelongsToMany): Promise { const response = await this.httpClient.request(`/toggle`, HttpMethod.PATCH, null, { - resources + resources, }); return new ToggleResult(response.data.attached, response.data.detached); @@ -106,7 +108,7 @@ export class BelongsToMany { const response = await this.httpClient.request(`/${key}/pivot`, HttpMethod.PATCH, null, { - pivot + pivot, }); return new UpdatePivotResult(response.data.updated); diff --git a/src/drivers/default/relations/hasMany.ts b/src/drivers/default/relations/hasMany.ts index c38a609..c052aff 100644 --- a/src/drivers/default/relations/hasMany.ts +++ b/src/drivers/default/relations/hasMany.ts @@ -1,22 +1,23 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {HttpMethod} from '../enums/httpMethod'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { RelationQueryBuilder } from '../builders/relationQueryBuilder'; +import { HttpMethod } from '../enums/httpMethod'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class HasMany, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends RelationQueryBuilder { + Relations = ExtractModelRelationsType +> extends RelationQueryBuilder { public async associate(key: string | number): Promise { const response = await this.httpClient.request( `/associate`, HttpMethod.POST, this.prepareQueryParams(), { - related_key: key + related_key: key, } ); diff --git a/src/drivers/default/relations/hasManyThrough.ts b/src/drivers/default/relations/hasManyThrough.ts index 8e52051..3d329a8 100644 --- a/src/drivers/default/relations/hasManyThrough.ts +++ b/src/drivers/default/relations/hasManyThrough.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {HasMany} from './hasMany'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { HasMany } from './hasMany'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class HasManyThrough, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends HasMany { -} + Relations = ExtractModelRelationsType +> extends HasMany {} diff --git a/src/drivers/default/relations/hasOne.ts b/src/drivers/default/relations/hasOne.ts index 42efe66..7530cf3 100644 --- a/src/drivers/default/relations/hasOne.ts +++ b/src/drivers/default/relations/hasOne.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { RelationQueryBuilder } from '../builders/relationQueryBuilder'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class HasOne, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, -> extends RelationQueryBuilder { -} + Relations = ExtractModelRelationsType +> extends RelationQueryBuilder {} diff --git a/src/drivers/default/relations/hasOneThrough.ts b/src/drivers/default/relations/hasOneThrough.ts index 338024e..87b54c1 100644 --- a/src/drivers/default/relations/hasOneThrough.ts +++ b/src/drivers/default/relations/hasOneThrough.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { RelationQueryBuilder } from '../builders/relationQueryBuilder'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class HasOneThrough, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends RelationQueryBuilder { -} + Relations = ExtractModelRelationsType +> extends RelationQueryBuilder {} diff --git a/src/drivers/default/relations/morphMany.ts b/src/drivers/default/relations/morphMany.ts index 90bf342..affcf05 100644 --- a/src/drivers/default/relations/morphMany.ts +++ b/src/drivers/default/relations/morphMany.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {HasMany} from './hasMany'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { HasMany } from './hasMany'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class MorphMany, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends HasMany { -} + Relations = ExtractModelRelationsType +> extends HasMany {} diff --git a/src/drivers/default/relations/morphOne.ts b/src/drivers/default/relations/morphOne.ts index e04a27f..4cd207a 100644 --- a/src/drivers/default/relations/morphOne.ts +++ b/src/drivers/default/relations/morphOne.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { RelationQueryBuilder } from '../builders/relationQueryBuilder'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class MorphOne, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends RelationQueryBuilder { -} + Relations = ExtractModelRelationsType +> extends RelationQueryBuilder {} diff --git a/src/drivers/default/relations/morphTo.ts b/src/drivers/default/relations/morphTo.ts index ab17606..8ed5995 100644 --- a/src/drivers/default/relations/morphTo.ts +++ b/src/drivers/default/relations/morphTo.ts @@ -1,12 +1,12 @@ -import {Model} from '../../../model'; -import {RelationQueryBuilder} from '../builders/relationQueryBuilder'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { RelationQueryBuilder } from '../builders/relationQueryBuilder'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class MorphTo, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends RelationQueryBuilder { -} + Relations = ExtractModelRelationsType +> extends RelationQueryBuilder {} diff --git a/src/drivers/default/relations/morphToMany.ts b/src/drivers/default/relations/morphToMany.ts index e8a4f76..d5dc1a3 100644 --- a/src/drivers/default/relations/morphToMany.ts +++ b/src/drivers/default/relations/morphToMany.ts @@ -1,13 +1,13 @@ -import {Model} from '../../../model'; -import {BelongsToMany} from './belongsToMany'; -import {ExtractModelAttributesType} from '../../../types/extractModelAttributesType'; -import {ExtractModelPersistedAttributesType} from '../../../types/extractModelPersistedAttributesType'; -import {ExtractModelRelationsType} from '../../../types/extractModelRelationsType'; +import { Model } from '../../../model'; +import { BelongsToMany } from './belongsToMany'; +import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType'; +import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType'; +import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType'; -export class MorphToMany, Attributes = ExtractModelAttributesType, PersistedAttributes = ExtractModelPersistedAttributesType, - Relations = ExtractModelRelationsType, - > extends BelongsToMany { -} + Relations = ExtractModelRelationsType +> extends BelongsToMany {} diff --git a/src/drivers/default/results/attachResult.ts b/src/drivers/default/results/attachResult.ts index 31b9ce5..9dcf45e 100644 --- a/src/drivers/default/results/attachResult.ts +++ b/src/drivers/default/results/attachResult.ts @@ -1,4 +1,3 @@ export class AttachResult { - constructor(public attached: Array) { - } + constructor(public attached: Array) {} } diff --git a/src/drivers/default/results/detachResult.ts b/src/drivers/default/results/detachResult.ts index 7eef38d..2966d33 100644 --- a/src/drivers/default/results/detachResult.ts +++ b/src/drivers/default/results/detachResult.ts @@ -1,4 +1,3 @@ export class DetachResult { - constructor(public detached: Array) { - } + constructor(public detached: Array) {} } diff --git a/src/drivers/default/results/syncResult.ts b/src/drivers/default/results/syncResult.ts index 40e09b9..e00f9a4 100644 --- a/src/drivers/default/results/syncResult.ts +++ b/src/drivers/default/results/syncResult.ts @@ -3,6 +3,5 @@ export class SyncResult { public attached: Array, public updated: Array, public detached: Array - ) { - } + ) {} } diff --git a/src/drivers/default/results/toggleResult.ts b/src/drivers/default/results/toggleResult.ts index d352036..60658bc 100644 --- a/src/drivers/default/results/toggleResult.ts +++ b/src/drivers/default/results/toggleResult.ts @@ -1,4 +1,3 @@ export class ToggleResult { - constructor(public attached: Array, public detached: Array) { - } + constructor(public attached: Array, public detached: Array) {} } diff --git a/src/drivers/default/results/updatePivotResult.ts b/src/drivers/default/results/updatePivotResult.ts index 664edb1..1a52edc 100644 --- a/src/drivers/default/results/updatePivotResult.ts +++ b/src/drivers/default/results/updatePivotResult.ts @@ -1,4 +1,3 @@ export class UpdatePivotResult { - constructor(public updated: Array) { - } + constructor(public updated: Array) {} } diff --git a/src/drivers/default/scope.ts b/src/drivers/default/scope.ts index 1515e0a..efc6a49 100644 --- a/src/drivers/default/scope.ts +++ b/src/drivers/default/scope.ts @@ -1,6 +1,5 @@ export class Scope { - constructor(protected name: string, protected parameters: Array = []) { - } + constructor(protected name: string, protected parameters: Array = []) {} public getName(): string { return this.name; diff --git a/src/drivers/default/sorter.ts b/src/drivers/default/sorter.ts index aa34c49..67084c9 100644 --- a/src/drivers/default/sorter.ts +++ b/src/drivers/default/sorter.ts @@ -1,8 +1,7 @@ -import {SortDirection} from './enums/sortDirection'; +import { SortDirection } from './enums/sortDirection'; export class Sorter { - constructor(protected field: string, protected direction: SortDirection = SortDirection.Asc) { - } + constructor(protected field: string, protected direction: SortDirection = SortDirection.Asc) {} public getField(): string { return this.field; diff --git a/src/httpClient.ts b/src/httpClient.ts index 2438d3e..e0c3e80 100644 --- a/src/httpClient.ts +++ b/src/httpClient.ts @@ -1,7 +1,7 @@ -import {HttpMethod} from './drivers/default/enums/httpMethod'; -import {Orion} from './orion'; -import {AuthDriver} from './drivers/default/enums/authDriver'; -import {AxiosInstance, AxiosRequestConfig, AxiosResponse} from 'axios'; +import { HttpMethod } from './drivers/default/enums/httpMethod'; +import { Orion } from './orion'; +import { AuthDriver } from './drivers/default/enums/authDriver'; +import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; export class HttpClient { protected static xsrfTokenFetched: boolean = false; @@ -27,7 +27,7 @@ export class HttpClient { baseURL: this.baseUrl, url, method, - params + params, }); if (method !== HttpMethod.GET) { @@ -43,7 +43,7 @@ export class HttpClient { } await this.client - .get(`sanctum/csrf-cookie`, {baseURL: Orion.getHost()}) + .get(`sanctum/csrf-cookie`, { baseURL: Orion.getHost() }) .then(() => { HttpClient.xsrfTokenFetched = true; }) diff --git a/src/model.ts b/src/model.ts index eb8674d..71dbde6 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,15 +1,17 @@ -import {QueryBuilder} from './drivers/default/builders/queryBuilder'; +import { QueryBuilder } from './drivers/default/builders/queryBuilder'; import pluralize from 'pluralize'; -import {noCase, snakeCase} from 'change-case'; -import {ModelConstructor} from './contracts/modelConstructor'; -import {AxiosResponse} from 'axios'; -import {DefaultPersistedAttributes} from "./types/defaultPersistedAttributes"; +import { noCase, snakeCase } from 'change-case'; +import { ModelConstructor } from './contracts/modelConstructor'; +import { AxiosResponse } from 'axios'; +import { DefaultPersistedAttributes } from './types/defaultPersistedAttributes'; -export abstract class Model, +export abstract class Model< + Attributes = Record, PersistedAttributes = Record | DefaultPersistedAttributes, Relations = Record, Key extends number | string = number | string, - AllAttributes = Attributes & PersistedAttributes> { + AllAttributes = Attributes & PersistedAttributes +> { public $attributes!: AllAttributes; public $relations!: Relations; @@ -38,10 +40,13 @@ export abstract class Model, public async $save(attributes?: Attributes): Promise { if (attributes) { - this.$setAttributes(attributes as unknown as AllAttributes); + this.$setAttributes((attributes as unknown) as AllAttributes); } - await this.$query().update(this.$getKey(), (attributes || this.$attributes) as Record); + await this.$query().update( + this.$getKey(), + (attributes || this.$attributes) as Record + ); return this; } diff --git a/src/orion.ts b/src/orion.ts index bcd5bf2..bf3a42a 100644 --- a/src/orion.ts +++ b/src/orion.ts @@ -1,6 +1,6 @@ -import {AuthDriver} from './drivers/default/enums/authDriver'; -import {HttpClient} from './httpClient'; -import axios, {AxiosInstance, AxiosRequestConfig} from 'axios'; +import { AuthDriver } from './drivers/default/enums/authDriver'; +import { HttpClient } from './httpClient'; +import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; export class Orion { protected static host: string; @@ -86,7 +86,9 @@ export class Orion { } public static makeHttpClient(baseUrl?: string): HttpClient { - const client: AxiosInstance = this.makeHttpClientCallback ? this.makeHttpClientCallback() : axios.create(); + const client: AxiosInstance = this.makeHttpClientCallback + ? this.makeHttpClientCallback() + : axios.create(); return new HttpClient(baseUrl || Orion.getApiUrl(), client); } @@ -99,12 +101,12 @@ export class Orion { protected static buildHttpClientConfig(): AxiosRequestConfig { const config: AxiosRequestConfig = { - withCredentials: Orion.getAuthDriver() === AuthDriver.Sanctum + withCredentials: Orion.getAuthDriver() === AuthDriver.Sanctum, }; if (Orion.getToken()) { config.headers = { - Authorization: `Bearer ${Orion.getToken()}` + Authorization: `Bearer ${Orion.getToken()}`, }; } diff --git a/src/types/defaultPersistedAttributesWithSoftDeletes.ts b/src/types/defaultPersistedAttributesWithSoftDeletes.ts index c8f8f3e..633761d 100644 --- a/src/types/defaultPersistedAttributesWithSoftDeletes.ts +++ b/src/types/defaultPersistedAttributesWithSoftDeletes.ts @@ -1,6 +1,6 @@ -import {DefaultPersistedAttributes} from './defaultPersistedAttributes'; +import { DefaultPersistedAttributes } from './defaultPersistedAttributes'; export type DefaultPersistedAttributesWithSoftDeletes = T & DefaultPersistedAttributes & { - deleted_at: string | null; -}; + deleted_at: string | null; + }; diff --git a/src/types/defaultSoftDeletablePersistedAttributes.ts b/src/types/defaultSoftDeletablePersistedAttributes.ts index 7550234..27339be 100644 --- a/src/types/defaultSoftDeletablePersistedAttributes.ts +++ b/src/types/defaultSoftDeletablePersistedAttributes.ts @@ -1,4 +1,4 @@ -import {DefaultPersistedAttributes} from './defaultPersistedAttributes'; +import { DefaultPersistedAttributes } from './defaultPersistedAttributes'; export type DefaultSoftDeletablePersistedAttributes = T & DefaultPersistedAttributes & { deleted_at: string | null }; diff --git a/src/types/extractModelAllAttributesType.ts b/src/types/extractModelAllAttributesType.ts index 5d2a303..47b08d7 100644 --- a/src/types/extractModelAllAttributesType.ts +++ b/src/types/extractModelAllAttributesType.ts @@ -1,9 +1,11 @@ -import {Model} from '../model'; +import { Model } from '../model'; -export type ExtractModelAllAttributesType = T extends Model +export type ExtractModelAllAttributesType = T extends Model< + infer Attributes, + infer PersistedAttributes, + infer Relations, + infer Key, + infer AllAttributes +> ? AllAttributes : never; diff --git a/src/types/extractModelAttributesType.ts b/src/types/extractModelAttributesType.ts index 152e983..084f97c 100644 --- a/src/types/extractModelAttributesType.ts +++ b/src/types/extractModelAttributesType.ts @@ -1,9 +1,11 @@ -import {Model} from '../model'; +import { Model } from '../model'; -export type ExtractModelAttributesType = T extends Model +export type ExtractModelAttributesType = T extends Model< + infer Attributes, + infer PersistedAttributes, + infer Relations, + infer Key, + infer AllAttributes +> ? Attributes : never; diff --git a/src/types/extractModelKeyType.ts b/src/types/extractModelKeyType.ts index 21fe9f4..64eb40d 100644 --- a/src/types/extractModelKeyType.ts +++ b/src/types/extractModelKeyType.ts @@ -1,9 +1,11 @@ -import {Model} from '../model'; +import { Model } from '../model'; -export type ExtractModelKeyType = T extends Model +export type ExtractModelKeyType = T extends Model< + infer Attributes, + infer PersistedAttributes, + infer Relations, + infer Key, + infer AllAttributes +> ? Key : never; diff --git a/src/types/extractModelPersistedAttributesType.ts b/src/types/extractModelPersistedAttributesType.ts index b5aa48d..8f63ddf 100644 --- a/src/types/extractModelPersistedAttributesType.ts +++ b/src/types/extractModelPersistedAttributesType.ts @@ -1,9 +1,11 @@ -import {Model} from '../model'; +import { Model } from '../model'; -export type ExtractModelPersistedAttributesType = T extends Model +export type ExtractModelPersistedAttributesType = T extends Model< + infer Attributes, + infer PersistedAttributes, + infer Relations, + infer Key, + infer AllAttributes +> ? PersistedAttributes : never; diff --git a/src/types/extractModelRelationsType.ts b/src/types/extractModelRelationsType.ts index 10f1faf..1def092 100644 --- a/src/types/extractModelRelationsType.ts +++ b/src/types/extractModelRelationsType.ts @@ -1,9 +1,11 @@ -import {Model} from '../model'; +import { Model } from '../model'; -export type ExtractModelRelationsType = T extends Model +export type ExtractModelRelationsType = T extends Model< + infer Attributes, + infer PersistedAttributes, + infer Relations, + infer Key, + infer AllAttributes +> ? Relations : never; diff --git a/tests/integration/builders/urlBuilder.test.ts b/tests/integration/builders/urlBuilder.test.ts index 5511b22..1439a31 100644 --- a/tests/integration/builders/urlBuilder.test.ts +++ b/tests/integration/builders/urlBuilder.test.ts @@ -1,10 +1,9 @@ -import {UrlBuilder} from "../../../src/builders/urlBuilder"; -import Post from "../../stubs/models/post"; -import {Orion} from "../../../src/orion"; -import User from "../../stubs/models/user"; +import { UrlBuilder } from '../../../src/builders/urlBuilder'; +import Post from '../../stubs/models/post'; +import { Orion } from '../../../src/orion'; +import User from '../../stubs/models/user'; describe('UriBuilder tests', () => { - test('building resource base url', () => { Orion.init('https://example.com'); @@ -16,12 +15,14 @@ describe('UriBuilder tests', () => { test('building resource url', () => { Orion.init('https://example.com'); - const resourceUrl = UrlBuilder.getResourceUrl(new Post({ - id: 1, - title: 'New Post', - created_at: null, - updated_at: null - })); + const resourceUrl = UrlBuilder.getResourceUrl( + new Post({ + id: 1, + title: 'New Post', + created_at: null, + updated_at: null, + }) + ); expect(resourceUrl).toBe('https://example.com/api/posts/1'); }); @@ -29,12 +30,15 @@ describe('UriBuilder tests', () => { test('building relation resource url', () => { Orion.init('https://example.com'); - const resourceUrl = UrlBuilder.getRelationResourceUrl(new User({ - id: 1, - name: 'New User', - created_at: null, - updated_at: null - }), Post); + const resourceUrl = UrlBuilder.getRelationResourceUrl( + new User({ + id: 1, + name: 'New User', + created_at: null, + updated_at: null, + }), + Post + ); expect(resourceUrl).toBe('https://example.com/api/users/1/posts'); }); diff --git a/tests/integration/drivers/default/builders/queryBuilder.test.ts b/tests/integration/drivers/default/builders/queryBuilder.test.ts index 9548a5c..deed85e 100644 --- a/tests/integration/drivers/default/builders/queryBuilder.test.ts +++ b/tests/integration/drivers/default/builders/queryBuilder.test.ts @@ -1,10 +1,10 @@ -import {QueryBuilder} from "../../../../../src/drivers/default/builders/queryBuilder"; +import { QueryBuilder } from '../../../../../src/drivers/default/builders/queryBuilder'; import Post from '../../../../stubs/models/post'; -import makeServer from "../server"; -import {FilterOperator} from "../../../../../src/drivers/default/enums/filterOperator"; -import {FilterType} from "../../../../../src/drivers/default/enums/filterType"; -import {SortDirection} from "../../../../../src/drivers/default/enums/sortDirection"; -import User from "../../../../stubs/models/user"; +import makeServer from '../server'; +import { FilterOperator } from '../../../../../src/drivers/default/enums/filterOperator'; +import { FilterType } from '../../../../../src/drivers/default/enums/filterType'; +import { SortDirection } from '../../../../../src/drivers/default/enums/sortDirection'; +import User from '../../../../stubs/models/user'; let server: any; @@ -13,18 +13,17 @@ beforeEach(() => { }); afterEach(() => { - server.shutdown() + server.shutdown(); }); describe('QueryBuilder tests', () => { - type PostAttributes = { - title: string + title: string; }; test('retrieving a paginated list of resources', async () => { - server.schema.posts.create({title: 'Test Post A'}); - server.schema.posts.create({title: 'Test Post B'}); + server.schema.posts.create({ title: 'Test Post A' }); + server.schema.posts.create({ title: 'Test Post B' }); const queryBuilder = new QueryBuilder(Post); const results = await queryBuilder.get(2, 5); @@ -33,13 +32,13 @@ describe('QueryBuilder tests', () => { expect(result).toBeInstanceOf(Post); }); - expect(results[0].$attributes).toStrictEqual({id: '1', title: 'Test Post A'}); - expect(results[1].$attributes).toStrictEqual({id: '2', title: 'Test Post B'}); + expect(results[0].$attributes).toStrictEqual({ id: '1', title: 'Test Post A' }); + expect(results[1].$attributes).toStrictEqual({ id: '2', title: 'Test Post B' }); expect(results.length).toBe(2); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({limit: '2', page: '5'}); + expect(requests[0].queryParams).toStrictEqual({ limit: '2', page: '5' }); }); test('retrieving a paginated list of only trashed resources', async () => { @@ -47,7 +46,7 @@ describe('QueryBuilder tests', () => { await queryBuilder.onlyTrashed().get(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({limit: '15', page: '1', only_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ limit: '15', page: '1', only_trashed: 'true' }); }); test('retrieving a paginated list resources with trashed ones', async () => { @@ -55,7 +54,7 @@ describe('QueryBuilder tests', () => { await queryBuilder.withTrashed().get(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({limit: '15', page: '1', with_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ limit: '15', page: '1', with_trashed: 'true' }); }); test('retrieving a paginated list resources with included relations', async () => { @@ -66,13 +65,13 @@ describe('QueryBuilder tests', () => { expect(requests[0].queryParams).toStrictEqual({ limit: '15', page: '1', - include: 'user,profile' + include: 'user,profile', }); }); test('searching for resources', async () => { - server.schema.posts.create({title: 'Test Post A'}); - server.schema.posts.create({title: 'Test Post B'}); + server.schema.posts.create({ title: 'Test Post A' }); + server.schema.posts.create({ title: 'Test Post B' }); const queryBuilder = new QueryBuilder(Post); const results = await queryBuilder @@ -86,20 +85,22 @@ describe('QueryBuilder tests', () => { expect(result).toBeInstanceOf(Post); }); - expect(results[0].$attributes).toStrictEqual({id: '1', title: 'Test Post A'}); - expect(results[1].$attributes).toStrictEqual({id: '2', title: 'Test Post B'}); + expect(results[0].$attributes).toStrictEqual({ id: '1', title: 'Test Post A' }); + expect(results[1].$attributes).toStrictEqual({ id: '2', title: 'Test Post B' }); const requests = server.pretender.handledRequests; const searchParameters = { - scopes: [{name: 'test scope', parameters: [1, 2, 3]}], - filters: [{ - field: 'test field', - operator: FilterOperator.GreaterThanOrEqual, - value: 'test value', - type: FilterType.Or - }], - search: {value: 'test keyword'}, - sort: [{field: 'test field', direction: SortDirection.Desc}] + scopes: [{ name: 'test scope', parameters: [1, 2, 3] }], + filters: [ + { + field: 'test field', + operator: FilterOperator.GreaterThanOrEqual, + value: 'test value', + type: FilterType.Or, + }, + ], + search: { value: 'test keyword' }, + sort: [{ field: 'test field', direction: SortDirection.Desc }], }; expect(JSON.parse(requests[0].requestBody)).toStrictEqual(searchParameters); }); @@ -109,7 +110,7 @@ describe('QueryBuilder tests', () => { await queryBuilder.onlyTrashed().search(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({limit: '15', page: '1', only_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ limit: '15', page: '1', only_trashed: 'true' }); }); test('searching for resources with trashed ones', async () => { @@ -117,7 +118,7 @@ describe('QueryBuilder tests', () => { await queryBuilder.withTrashed().search(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({limit: '15', page: '1', with_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ limit: '15', page: '1', with_trashed: 'true' }); }); test('searching for resources with included relations', async () => { @@ -128,175 +129,183 @@ describe('QueryBuilder tests', () => { expect(requests[0].queryParams).toStrictEqual({ limit: '15', page: '1', - include: 'user,profile' + include: 'user,profile', }); }); test('storing a resource', async () => { const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.store({ - title: 'Test Post' + title: 'Test Post', }); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post' }); expect(server.schema.posts.find('1').attrs.title).toBe('Test Post'); }); test('storing a resource and getting its relations', async () => { const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.with(['user', 'profile']).store({ - title: 'Test Post' + title: 'Test Post', }); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post' }); expect(server.schema.posts.find('1').attrs.title).toBe('Test Post'); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({include: 'user,profile'}); + expect(requests[0].queryParams).toStrictEqual({ include: 'user,profile' }); }); test('retrieving a resource', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.find('1'); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post' }); }); test('retrieving a soft deleted resource', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); await queryBuilder.withTrashed().find('1'); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({with_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ with_trashed: 'true' }); }); test('retrieving a resource with included relations', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.with(['user', 'profile']).find('1'); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post' }); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({include: 'user,profile'}); + expect(requests[0].queryParams).toStrictEqual({ include: 'user,profile' }); }); test('updating a resource', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.update('1', { - title: 'Updated Post' + title: 'Updated Post', }); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Updated Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Updated Post' }); expect(server.schema.posts.find('1').attrs.title).toBe('Updated Post'); }); test('updating a resource with included relations', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.with(['user', 'profile']).update('1', { - title: 'Updated Post' + title: 'Updated Post', }); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Updated Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Updated Post' }); expect(server.schema.posts.find('1').attrs.title).toBe('Updated Post'); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({include: 'user,profile'}); + expect(requests[0].queryParams).toStrictEqual({ include: 'user,profile' }); }); test('updating a soft deleted resource', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.withTrashed().update('1', { - title: 'Updated Post' + title: 'Updated Post', }); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Updated Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Updated Post' }); expect(server.schema.posts.find('1').attrs.title).toBe('Updated Post'); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({with_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ with_trashed: 'true' }); }); test('trashing a resource', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.destroy('1'); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post', deleted_at: '2021-01-01'}); + expect(post.$attributes).toStrictEqual({ + id: '1', + title: 'Test Post', + deleted_at: '2021-01-01', + }); expect(server.schema.posts.find('1').attrs.deleted_at).toBeDefined(); }); test('trashing a resource with included relations', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.with(['user', 'profile']).destroy('1'); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post', deleted_at: '2021-01-01'}); + expect(post.$attributes).toStrictEqual({ + id: '1', + title: 'Test Post', + deleted_at: '2021-01-01', + }); expect(server.schema.posts.find('1').attrs.deleted_at).toBeDefined(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({force: 'false', include: 'user,profile'}); + expect(requests[0].queryParams).toStrictEqual({ force: 'false', include: 'user,profile' }); }); test('restoring a resource', async () => { - server.schema.posts.create({title: 'Test Post', deleted_at: Date.now()}); + server.schema.posts.create({ title: 'Test Post', deleted_at: Date.now() }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.restore('1'); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post', deleted_at: null}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post', deleted_at: null }); expect(server.schema.posts.find('1').attrs.deleted_at).toBeNull(); }); test('restoring a resource with included relations', async () => { - server.schema.posts.create({title: 'Test Post', deleted_at: Date.now()}); + server.schema.posts.create({ title: 'Test Post', deleted_at: Date.now() }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.with(['user', 'profile']).restore('1'); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post', deleted_at: null}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post', deleted_at: null }); expect(server.schema.posts.find('1').attrs.deleted_at).toBeNull(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({include: 'user,profile'}); + expect(requests[0].queryParams).toStrictEqual({ include: 'user,profile' }); }); test('force deleting a resource', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const queryBuilder = new QueryBuilder(Post); const post = await queryBuilder.destroy('1', true); expect(post).toBeInstanceOf(Post); - expect(post.$attributes).toStrictEqual({id: '1', title: 'Test Post'}); + expect(post.$attributes).toStrictEqual({ id: '1', title: 'Test Post' }); expect(server.schema.posts.find('1')).toBeNull(); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({force: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ force: 'true' }); }); test('hydrating model with attributes and relations', async () => { @@ -306,12 +315,12 @@ describe('QueryBuilder tests', () => { title: 'test', updated_at: '2021-02-01', created_at: '2021-02-01', - user: { + user: ({ id: 1, name: 'Test User', updated_at: '2021-02-01', - created_at: '2021-02-01' - } as unknown as User + created_at: '2021-02-01', + } as unknown) as User, }); expect(post).toBeInstanceOf(Post); @@ -326,10 +335,7 @@ describe('QueryBuilder tests', () => { id: 1, name: 'Test User', updated_at: '2021-02-01', - created_at: '2021-02-01' + created_at: '2021-02-01', }); }); - }); - - diff --git a/tests/integration/drivers/default/relations/belongsToMany.test.ts b/tests/integration/drivers/default/relations/belongsToMany.test.ts index 315d53f..5057633 100644 --- a/tests/integration/drivers/default/relations/belongsToMany.test.ts +++ b/tests/integration/drivers/default/relations/belongsToMany.test.ts @@ -1,12 +1,12 @@ import Post from '../../../../stubs/models/post'; -import makeServer from "../server"; -import {BelongsToMany} from "../../../../../src/drivers/default/relations/belongsToMany"; -import Tag from "../../../../stubs/models/tag"; -import {AttachResult} from "../../../../../src/drivers/default/results/attachResult"; -import {DetachResult} from "../../../../../src/drivers/default/results/detachResult"; -import {SyncResult} from "../../../../../src/drivers/default/results/syncResult"; -import {ToggleResult} from "../../../../../src/drivers/default/results/toggleResult"; -import {UpdatePivotResult} from "../../../../../src/drivers/default/results/updatePivotResult"; +import makeServer from '../server'; +import { BelongsToMany } from '../../../../../src/drivers/default/relations/belongsToMany'; +import Tag from '../../../../stubs/models/tag'; +import { AttachResult } from '../../../../../src/drivers/default/results/attachResult'; +import { DetachResult } from '../../../../../src/drivers/default/results/detachResult'; +import { SyncResult } from '../../../../../src/drivers/default/results/syncResult'; +import { ToggleResult } from '../../../../../src/drivers/default/results/toggleResult'; +import { UpdatePivotResult } from '../../../../../src/drivers/default/results/updatePivotResult'; let server: any; @@ -15,21 +15,20 @@ beforeEach(() => { }); afterEach(() => { - server.shutdown() + server.shutdown(); }); describe('BelongsToMany tests', () => { - type TagAttributes = { - content: string + content: string; }; type TagPivot = { - example_pivot_field: string + example_pivot_field: string; }; test('attaching resources', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); @@ -40,11 +39,11 @@ describe('BelongsToMany tests', () => { expect(attachResult.attached).toStrictEqual([2, 5, 7]); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({duplicates: 'false'}); + expect(requests[0].queryParams).toStrictEqual({ duplicates: 'false' }); }); test('attaching resources with duplicates', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); @@ -52,52 +51,55 @@ describe('BelongsToMany tests', () => { await belongsToManyRelation.attach([2, 5, 7], true); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({duplicates: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ duplicates: 'true' }); }); test('attaching resources with fields', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); const attachResult = await belongsToManyRelation.attachWithFields({ 2: { - example_pivot_field: 'value A' + example_pivot_field: 'value A', }, 5: { - example_pivot_field: "value B" - } + example_pivot_field: 'value B', + }, }); expect(attachResult).toBeInstanceOf(AttachResult); expect(attachResult.attached).toStrictEqual(['2', '5']); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({duplicates: 'false'}); + expect(requests[0].queryParams).toStrictEqual({ duplicates: 'false' }); }); test('attaching resources with fields with duplicates', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); - await belongsToManyRelation.attachWithFields({ - 2: { - example_pivot_field: 'value A' + await belongsToManyRelation.attachWithFields( + { + 2: { + example_pivot_field: 'value A', + }, + 5: { + example_pivot_field: 'value B', + }, }, - 5: { - example_pivot_field: "value B" - } - }, true); + true + ); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({duplicates: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ duplicates: 'true' }); }); test('detaching resources', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); @@ -109,18 +111,18 @@ describe('BelongsToMany tests', () => { }); test('detaching resources with fields', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); const detachResult = await belongsToManyRelation.detachWithFields({ 2: { - example_pivot_field: 'value A' + example_pivot_field: 'value A', }, 5: { - example_pivot_field: "value B" - } + example_pivot_field: 'value B', + }, }); expect(detachResult).toBeInstanceOf(DetachResult); @@ -128,7 +130,7 @@ describe('BelongsToMany tests', () => { }); test('syncing resources', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); @@ -141,11 +143,11 @@ describe('BelongsToMany tests', () => { expect(syncResult.detached).toStrictEqual([2, 5, 7]); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({detaching: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ detaching: 'true' }); }); test('syncing resources without detaching', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); @@ -153,22 +155,22 @@ describe('BelongsToMany tests', () => { await belongsToManyRelation.sync([2, 5, 7], false); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({detaching: 'false'}); + expect(requests[0].queryParams).toStrictEqual({ detaching: 'false' }); }); test('syncing resources with fields', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); const syncResult = await belongsToManyRelation.syncWithFields({ 2: { - example_pivot_field: 'value A' + example_pivot_field: 'value A', }, 5: { - example_pivot_field: "value B" - } + example_pivot_field: 'value B', + }, }); expect(syncResult).toBeInstanceOf(SyncResult); @@ -177,30 +179,33 @@ describe('BelongsToMany tests', () => { expect(syncResult.detached).toStrictEqual(['2', '5']); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({detaching: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ detaching: 'true' }); }); test('syncing resources with fields without detaching', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); - await belongsToManyRelation.syncWithFields({ - 2: { - example_pivot_field: 'value A' + await belongsToManyRelation.syncWithFields( + { + 2: { + example_pivot_field: 'value A', + }, + 5: { + example_pivot_field: 'value B', + }, }, - 5: { - example_pivot_field: "value B" - } - }, false); + false + ); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({detaching: 'false'}); + expect(requests[0].queryParams).toStrictEqual({ detaching: 'false' }); }); test('toggling resources', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); @@ -213,18 +218,18 @@ describe('BelongsToMany tests', () => { }); test('toggling resources with fields', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); const toggleResult = await belongsToManyRelation.toggleWithFields({ 2: { - example_pivot_field: 'value A' + example_pivot_field: 'value A', }, 5: { - example_pivot_field: "value B" - } + example_pivot_field: 'value B', + }, }); expect(toggleResult).toBeInstanceOf(ToggleResult); @@ -233,16 +238,16 @@ describe('BelongsToMany tests', () => { }); test('updating resource pivot', async () => { - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const post = new Post(postEntity.attrs); const belongsToManyRelation = new BelongsToMany(Tag, post); - const updatePivotResult = await belongsToManyRelation.updatePivot(5, {example_pivot_field : 'value'}); + const updatePivotResult = await belongsToManyRelation.updatePivot(5, { + example_pivot_field: 'value', + }); expect(updatePivotResult).toBeInstanceOf(UpdatePivotResult); expect(updatePivotResult.updated).toStrictEqual(['5']); }); }); - - diff --git a/tests/integration/drivers/default/relations/hasMany.test.ts b/tests/integration/drivers/default/relations/hasMany.test.ts index e9a6aed..bdc1d1f 100644 --- a/tests/integration/drivers/default/relations/hasMany.test.ts +++ b/tests/integration/drivers/default/relations/hasMany.test.ts @@ -1,7 +1,7 @@ -import {HasMany} from "../../../../../src/drivers/default/relations/hasMany"; +import { HasMany } from '../../../../../src/drivers/default/relations/hasMany'; import Post from '../../../../stubs/models/post'; -import makeServer from "../server"; -import User from "../../../../stubs/models/user"; +import makeServer from '../server'; +import User from '../../../../stubs/models/user'; let server: any; @@ -10,18 +10,17 @@ beforeEach(() => { }); afterEach(() => { - server.shutdown() + server.shutdown(); }); describe('HasMany tests', () => { - type PostAttributes = { - title: string + title: string; }; test('associating a resource', async () => { - const userEntity = server.schema.users.create({name: 'Test User'}); - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const userEntity = server.schema.users.create({ name: 'Test User' }); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const user = new User(userEntity.attrs); @@ -29,13 +28,13 @@ describe('HasMany tests', () => { const associatedPost = await hasManyRelation.associate(postEntity.attrs.id); expect(associatedPost).toBeInstanceOf(Post); - expect(associatedPost.$attributes).toStrictEqual({id: '1', title: 'Test Post', user_id: '1'}); + expect(associatedPost.$attributes).toStrictEqual({ id: '1', title: 'Test Post', user_id: '1' }); expect(server.schema.posts.find('1').attrs.user_id).toBe('1'); }); test('associating a soft deleted resource', async () => { - const userEntity = server.schema.users.create({name: 'Test User'}); - const postEntity = server.schema.posts.create({title: 'Test Post'}); + const userEntity = server.schema.users.create({ name: 'Test User' }); + const postEntity = server.schema.posts.create({ title: 'Test Post' }); const user = new User(userEntity.attrs); @@ -43,12 +42,15 @@ describe('HasMany tests', () => { await hasManyRelation.withTrashed().associate(postEntity.attrs.id); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({with_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ with_trashed: 'true' }); }); test('dissociating a resource', async () => { - const userEntity = server.schema.users.create({name: 'Test User'}); - const postEntity = server.schema.posts.create({title: 'Test Post', user_id: userEntity.attrs.id}); + const userEntity = server.schema.users.create({ name: 'Test User' }); + const postEntity = server.schema.posts.create({ + title: 'Test Post', + user_id: userEntity.attrs.id, + }); const user = new User(userEntity.attrs); @@ -56,13 +58,20 @@ describe('HasMany tests', () => { const associatedPost = await hasManyRelation.dissociate(postEntity.attrs.id); expect(associatedPost).toBeInstanceOf(Post); - expect(associatedPost.$attributes).toStrictEqual({id: '1', title: 'Test Post', user_id: null}); + expect(associatedPost.$attributes).toStrictEqual({ + id: '1', + title: 'Test Post', + user_id: null, + }); expect(server.schema.posts.find('1').attrs.user_id).toBeNull(); }); test('dissociating a soft deleted resource', async () => { - const userEntity = server.schema.users.create({name: 'Test User'}); - const postEntity = server.schema.posts.create({title: 'Test Post', user_id: userEntity.attrs.id}); + const userEntity = server.schema.users.create({ name: 'Test User' }); + const postEntity = server.schema.posts.create({ + title: 'Test Post', + user_id: userEntity.attrs.id, + }); const user = new User(userEntity.attrs); @@ -70,8 +79,6 @@ describe('HasMany tests', () => { await hasManyRelation.withTrashed().dissociate(postEntity.attrs.id); const requests = server.pretender.handledRequests; - expect(requests[0].queryParams).toStrictEqual({with_trashed: 'true'}); + expect(requests[0].queryParams).toStrictEqual({ with_trashed: 'true' }); }); }); - - diff --git a/tests/integration/drivers/default/serializer.ts b/tests/integration/drivers/default/serializer.ts index 47abb8e..5d811d4 100644 --- a/tests/integration/drivers/default/serializer.ts +++ b/tests/integration/drivers/default/serializer.ts @@ -1,5 +1,5 @@ -import {RestSerializer} from "miragejs"; -import {snakeCase} from "change-case"; +import { RestSerializer } from 'miragejs'; +import { snakeCase } from 'change-case'; export const LaravelSerializer = RestSerializer.extend({ keyForCollection() { @@ -11,4 +11,4 @@ export const LaravelSerializer = RestSerializer.extend({ keyForAttribute(attr) { return snakeCase(attr); }, -}) +}); diff --git a/tests/integration/drivers/default/server.ts b/tests/integration/drivers/default/server.ts index 26c363b..2c73cfb 100644 --- a/tests/integration/drivers/default/server.ts +++ b/tests/integration/drivers/default/server.ts @@ -1,6 +1,6 @@ -import {belongsTo, createServer, hasMany, Model as MirageModel} from "miragejs"; -import {Orion} from "../../../../src/orion"; -import {LaravelSerializer} from './serializer'; +import { belongsTo, createServer, hasMany, Model as MirageModel } from 'miragejs'; +import { Orion } from '../../../../src/orion'; +import { LaravelSerializer } from './serializer'; export default function makeServer() { return createServer({ @@ -14,117 +14,117 @@ export default function makeServer() { models: { post: MirageModel.extend({ - user: belongsTo('user') + user: belongsTo('user'), }), user: MirageModel.extend({ - posts: hasMany('posts') - }) + posts: hasMany('posts'), + }), }, routes: function () { this.urlPrefix = 'https://api-mock.test'; this.namespace = ''; - this.get("/sanctum/csrf-cookie", () => { + this.get('/sanctum/csrf-cookie', () => { return []; }); - this.get("/api/posts"); + this.get('/api/posts'); - this.post("/api/posts", function (schema: any, request) { + this.post('/api/posts', function (schema: any, request) { const attrs = JSON.parse(request.requestBody); return schema.posts.create(attrs); }); - this.post("/api/posts/search", function (schema: any, request) { + this.post('/api/posts/search', function (schema: any, request) { return schema.posts.all(); }); - this.get("/api/posts/:id"); - this.patch("/api/posts/:id", (schema: any, request) => { - const id = request.params.id + this.get('/api/posts/:id'); + this.patch('/api/posts/:id', (schema: any, request) => { + const id = request.params.id; const attrs = JSON.parse(request.requestBody); const post = schema.posts.find(id); - return post.update(attrs) + return post.update(attrs); }); - this.del("/api/posts/:id", (schema: any, request) => { + this.del('/api/posts/:id', (schema: any, request) => { const id = request.params.id; const post = schema.posts.find(id); if (request.queryParams.force === 'true') { post.destroy(); } else { - post.update({deleted_at: '2021-01-01'}); + post.update({ deleted_at: '2021-01-01' }); } return post; }); - this.post("/api/posts/:id/restore", (schema: any, request) => { + this.post('/api/posts/:id/restore', (schema: any, request) => { const id = request.params.id; const post = schema.posts.find(id); - return post.update({deleted_at: null}); + return post.update({ deleted_at: null }); }); - this.post("/api/users/:id/posts/associate", (schema: any, request) => { + this.post('/api/users/:id/posts/associate', (schema: any, request) => { const userId = request.params.id; const postId = JSON.parse(request.requestBody).related_key; const post = schema.posts.find(postId); - return post.update({user_id: userId}); + return post.update({ user_id: userId }); }); - this.delete("/api/users/:user_id/posts/:post_id/dissociate", (schema: any, request) => { + this.delete('/api/users/:user_id/posts/:post_id/dissociate', (schema: any, request) => { const postId = request.params.post_id; const post = schema.posts.find(postId); - return post.update({user_id: null}); + return post.update({ user_id: null }); }); - this.post("/api/posts/:id/tags/attach", (schema: any, request) => { + this.post('/api/posts/:id/tags/attach', (schema: any, request) => { const tagIds = JSON.parse(request.requestBody).resources; return { - attached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds) - } + attached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), + }; }); - this.delete("/api/posts/:id/tags/detach", (schema: any, request) => { + this.delete('/api/posts/:id/tags/detach', (schema: any, request) => { const tagIds = JSON.parse(request.requestBody).resources; return { - detached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds) - } + detached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), + }; }); - this.patch("/api/posts/:id/tags/sync", (schema: any, request) => { + this.patch('/api/posts/:id/tags/sync', (schema: any, request) => { const tagIds = JSON.parse(request.requestBody).resources; return { attached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), updated: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), detached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), - } + }; }); - this.patch("/api/posts/:id/tags/toggle", (schema: any, request) => { + this.patch('/api/posts/:id/tags/toggle', (schema: any, request) => { const tagIds = JSON.parse(request.requestBody).resources; return { attached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), detached: Array.isArray(tagIds) ? tagIds : Object.keys(tagIds), - } + }; }); - this.patch("/api/posts/:post_id/tags/:tag_id/pivot", (schema: any, request) => { + this.patch('/api/posts/:post_id/tags/:tag_id/pivot', (schema: any, request) => { return { - updated: [request.params.tag_id] - } + updated: [request.params.tag_id], + }; }); }, }); diff --git a/tests/integration/httpClient.test.ts b/tests/integration/httpClient.test.ts index aa65444..d437966 100644 --- a/tests/integration/httpClient.test.ts +++ b/tests/integration/httpClient.test.ts @@ -1,7 +1,7 @@ -import {Orion} from "../../src/orion"; -import makeServer from "./drivers/default/server"; -import {HttpMethod} from "../../src/drivers/default/enums/httpMethod"; -import {AuthDriver} from "../../src/drivers/default/enums/authDriver"; +import { Orion } from '../../src/orion'; +import makeServer from './drivers/default/server'; +import { HttpMethod } from '../../src/drivers/default/enums/httpMethod'; +import { AuthDriver } from '../../src/drivers/default/enums/authDriver'; let server: any; @@ -10,13 +10,12 @@ beforeEach(() => { }); afterEach(() => { - server.shutdown() + server.shutdown(); }); describe('HttpClient tests', () => { - test('using bearer token', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); Orion.setToken('test'); await Orion.makeHttpClient().request('/posts', HttpMethod.GET); @@ -26,7 +25,7 @@ describe('HttpClient tests', () => { }); test('prefetching xsrf token', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); Orion.setAuthDriver(AuthDriver.Sanctum); await Orion.makeHttpClient().request('/posts', HttpMethod.GET); diff --git a/tests/integration/model.test.ts b/tests/integration/model.test.ts index 445c2da..48763fe 100644 --- a/tests/integration/model.test.ts +++ b/tests/integration/model.test.ts @@ -1,5 +1,5 @@ -import makeServer from "./drivers/default/server"; -import Post from "../stubs/models/post"; +import makeServer from './drivers/default/server'; +import Post from '../stubs/models/post'; let server: any; @@ -8,13 +8,12 @@ beforeEach(() => { }); afterEach(() => { - server.shutdown() + server.shutdown(); }); describe('Model tests', () => { - test('saving a model', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const post = await Post.$query().find(1); @@ -25,7 +24,7 @@ describe('Model tests', () => { }); test('trashing a model', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const post = await Post.$query().find(1); @@ -35,7 +34,7 @@ describe('Model tests', () => { }); test('force deleting a model', async () => { - server.schema.posts.create({title: 'Test Post'}); + server.schema.posts.create({ title: 'Test Post' }); const post = await Post.$query().find(1); @@ -44,5 +43,3 @@ describe('Model tests', () => { expect(server.schema.posts.find('1')).toBeNull(); }); }); - - diff --git a/tests/stubs/models/post.ts b/tests/stubs/models/post.ts index f12ca61..8d03f48 100644 --- a/tests/stubs/models/post.ts +++ b/tests/stubs/models/post.ts @@ -1,14 +1,18 @@ -import {Model} from "../../../src/model"; -import User from "./user"; -import {BelongsTo} from "../../../src/drivers/default/relations/belongsTo"; -import {DefaultPersistedAttributes} from "../../../src/types/defaultPersistedAttributes"; +import { Model } from '../../../src/model'; +import User from './user'; +import { BelongsTo } from '../../../src/drivers/default/relations/belongsTo'; +import { DefaultPersistedAttributes } from '../../../src/types/defaultPersistedAttributes'; -export default class Post extends Model<{ - title: string, -}, DefaultPersistedAttributes, { - user: User -}> { - public user() : BelongsTo { +export default class Post extends Model< + { + title: string; + }, + DefaultPersistedAttributes, + { + user: User; + } +> { + public user(): BelongsTo { return new BelongsTo(User, this); } } diff --git a/tests/stubs/models/tag.ts b/tests/stubs/models/tag.ts index 9fbdc26..a1e7f69 100644 --- a/tests/stubs/models/tag.ts +++ b/tests/stubs/models/tag.ts @@ -1,11 +1,11 @@ -import {Model} from "../../../src/model"; -import Post from "./post"; -import {BelongsToMany} from "../../../src/drivers/default/relations/belongsToMany"; +import { Model } from '../../../src/model'; +import Post from './post'; +import { BelongsToMany } from '../../../src/drivers/default/relations/belongsToMany'; export default class Tag extends Model<{ - content: string, + content: string; }> { - public posts() : BelongsToMany { + public posts(): BelongsToMany { return new BelongsToMany(Post, this); } } diff --git a/tests/stubs/models/user.ts b/tests/stubs/models/user.ts index 3dc83d1..6200766 100644 --- a/tests/stubs/models/user.ts +++ b/tests/stubs/models/user.ts @@ -1,7 +1,5 @@ -import {Model} from "../../../src/model"; +import { Model } from '../../../src/model'; export default class User extends Model<{ - name: string -}> { - -} + name: string; +}> {} diff --git a/tests/unit/model.test.ts b/tests/unit/model.test.ts index c386050..9e8b6f2 100644 --- a/tests/unit/model.test.ts +++ b/tests/unit/model.test.ts @@ -1,10 +1,9 @@ -import {Orion} from "../../src/orion"; -import Post from "../stubs/models/post"; +import { Orion } from '../../src/orion'; +import Post from '../stubs/models/post'; Orion.setHost('https://example.com/api'); describe('Model tests', () => { - test('setting and getting key name', () => { const model = new Post(); model.$setKeyName('custom_key'); @@ -22,5 +21,4 @@ describe('Model tests', () => { test('getting resource name', () => { expect(Post.prototype.$getResourceName()).toBe('posts'); }); - }); diff --git a/tests/unit/orion.test.ts b/tests/unit/orion.test.ts index aaa5be7..aae251b 100644 --- a/tests/unit/orion.test.ts +++ b/tests/unit/orion.test.ts @@ -1,8 +1,7 @@ -import {Orion} from "../../src/orion"; -import {AuthDriver} from "../../src/drivers/default/enums/authDriver"; +import { Orion } from '../../src/orion'; +import { AuthDriver } from '../../src/drivers/default/enums/authDriver'; describe('Orion tests', () => { - test('initialization', () => { Orion.init('https://example.com', 'custom-prefix', AuthDriver.Passport, 'test-token'); @@ -49,5 +48,4 @@ describe('Orion tests', () => { expect(Orion.getHost()).toBe('https://example.com/api/'); }); - });