From b6d656d155bff7c1b4200ba7dbd9f22d741399bf Mon Sep 17 00:00:00 2001 From: Pierluigi Viti Date: Tue, 9 Apr 2024 16:03:29 +0200 Subject: [PATCH] fix: fix query generics --- gen/templates/create.tpl | 2 +- gen/templates/list.tpl | 2 +- gen/templates/retrieve.tpl | 2 +- gen/templates/singleton.tpl | 2 +- gen/templates/singleton_update.tpl | 2 +- gen/templates/update.tpl | 2 +- src/query.ts | 6 +++--- src/resource.ts | 18 +++++++++--------- src/resources/api_credentials.ts | 5 +++-- src/resources/application_memberships.ts | 5 +++-- src/resources/memberships.ts | 5 +++-- src/resources/organizations.ts | 5 +++-- src/resources/permissions.ts | 5 +++-- src/resources/roles.ts | 5 +++-- src/resources/user.ts | 3 ++- src/util.ts | 2 +- 16 files changed, 39 insertions(+), 32 deletions(-) diff --git a/gen/templates/create.tpl b/gen/templates/create.tpl index f6865b6..75530b3 100644 --- a/gen/templates/create.tpl +++ b/gen/templates/create.tpl @@ -1,3 +1,3 @@ -async create(resource: ##__RESOURCE_REQUEST_CLASS__##, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { +async create(resource: ##__RESOURCE_REQUEST_CLASS__##, params?: QueryParamsRetrieve<##__RESOURCE_RESPONSE_CLASS__##>, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { return this.resources.create<##__RESOURCE_REQUEST_CLASS__##, ##__RESOURCE_RESPONSE_CLASS__##>({ ...resource, type: ##__RESOURCE_CLASS__##.TYPE }, params, options) } \ No newline at end of file diff --git a/gen/templates/list.tpl b/gen/templates/list.tpl index 666b1c7..bfe04e2 100644 --- a/gen/templates/list.tpl +++ b/gen/templates/list.tpl @@ -1,3 +1,3 @@ -async list(params?: QueryParamsList, options?: ResourcesConfig): Promise> { +async list(params?: QueryParamsList<##__RESOURCE_RESPONSE_CLASS__##>, options?: ResourcesConfig): Promise> { return this.resources.list<##__RESOURCE_RESPONSE_CLASS__##>({ type: ##__RESOURCE_CLASS__##.TYPE }, params, options) } \ No newline at end of file diff --git a/gen/templates/retrieve.tpl b/gen/templates/retrieve.tpl index 98f6717..561d77a 100644 --- a/gen/templates/retrieve.tpl +++ b/gen/templates/retrieve.tpl @@ -1,3 +1,3 @@ -async retrieve(id: string, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { +async retrieve(id: string, params?: QueryParamsRetrieve<##__RESOURCE_RESPONSE_CLASS__##>, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { return this.resources.retrieve<##__RESOURCE_RESPONSE_CLASS__##>({ type: ##__RESOURCE_CLASS__##.TYPE, id }, params, options) } \ No newline at end of file diff --git a/gen/templates/singleton.tpl b/gen/templates/singleton.tpl index 574eca7..6449aaf 100644 --- a/gen/templates/singleton.tpl +++ b/gen/templates/singleton.tpl @@ -1,3 +1,3 @@ -async retrieve(params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { +async retrieve(params?: QueryParamsRetrieve<##__RESOURCE_RESPONSE_CLASS__##>, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { return this.resources.retrieve<##__RESOURCE_RESPONSE_CLASS__##>({ type: ##__RESOURCE_CLASS__##.TYPE }, params, options) } \ No newline at end of file diff --git a/gen/templates/singleton_update.tpl b/gen/templates/singleton_update.tpl index b835dd8..f75219e 100644 --- a/gen/templates/singleton_update.tpl +++ b/gen/templates/singleton_update.tpl @@ -1,4 +1,4 @@ -async update(resource: ##__RESOURCE_REQUEST_CLASS__##, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { +async update(resource: ##__RESOURCE_REQUEST_CLASS__##, params?: QueryParamsRetrieve<##__RESOURCE_RESPONSE_CLASS__##>, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { const res = await this.retrieve(params, options) // JsonAPI requires id in the request body return this.resources.update<##__RESOURCE_REQUEST_CLASS__##, ##__RESOURCE_RESPONSE_CLASS__##>({ ...resource, id: res.id, type: ##__RESOURCE_CLASS__##.TYPE }, params, options) } \ No newline at end of file diff --git a/gen/templates/update.tpl b/gen/templates/update.tpl index 66111db..f2182ef 100644 --- a/gen/templates/update.tpl +++ b/gen/templates/update.tpl @@ -1,3 +1,3 @@ -async update(resource: ##__RESOURCE_REQUEST_CLASS__##, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { +async update(resource: ##__RESOURCE_REQUEST_CLASS__##, params?: QueryParamsRetrieve<##__RESOURCE_RESPONSE_CLASS__##>, options?: ResourcesConfig): Promise<##__RESOURCE_RESPONSE_CLASS__##> { return this.resources.update<##__RESOURCE_REQUEST_CLASS__##, ##__RESOURCE_RESPONSE_CLASS__##>({ ...resource, type: ##__RESOURCE_CLASS__##.TYPE }, params, options) } \ No newline at end of file diff --git a/src/query.ts b/src/query.ts index 0f45844..dba6007 100644 --- a/src/query.ts +++ b/src/query.ts @@ -19,7 +19,7 @@ type QueryResourceFields = keyof ResourceFields[R] type QueryArrayFields = Array>> type QueryRecordFields = { [key in keyof ResourceFields]?: Array<(QueryResourceFields)> } -interface QueryParamsRetrieve { +interface QueryParamsRetrieve { include?: QueryInclude fields?: QueryArrayFields | QueryRecordFields } @@ -33,14 +33,14 @@ type QueryFilter = Record -interface QueryParamsList extends QueryParamsRetrieve { +interface QueryParamsList extends QueryParamsRetrieve { sort?: QueryArraySortable | QueryRecordSortable filters?: QueryFilter pageNumber?: QueryPageNumber pageSize?: QueryPageSize } -type QueryParams = QueryParamsRetrieve | QueryParamsList +type QueryParams = QueryParamsRetrieve | QueryParamsList export type { QueryParamsRetrieve, QueryParamsList, QueryParams, QueryFilter, QueryPageSize } diff --git a/src/resource.ts b/src/resource.ts index 122f969..4ec738f 100644 --- a/src/resource.ts +++ b/src/resource.ts @@ -148,7 +148,7 @@ class ResourceAdapter { - async retrieve(resource: ResourceId | ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async retrieve(resource: ResourceId | ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { const singleton = !('id' in resource) || CommerceLayerProvisioningStatic.isSingleton(resource.type) @@ -167,7 +167,7 @@ class ResourceAdapter { } - async list(resource: ResourceType, params?: QueryParamsList, options?: ResourcesConfig): Promise> { + async list(resource: ResourceType, params?: QueryParamsList, options?: ResourcesConfig): Promise> { debug('list: %o, %O, %O', resource, params || {}, options || {}) @@ -205,7 +205,7 @@ class ResourceAdapter { } - async update(resource: U & ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: U & ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { const singleton = !('id' in resource) || CommerceLayerProvisioningStatic.isSingleton(resource.type) @@ -308,7 +308,7 @@ abstract class ApiResourceBase { // reference, reference_origin and metadata attributes are always updatable - async update(resource: ResourceUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: ResourceUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: this.type() }, params, options) } @@ -317,16 +317,16 @@ abstract class ApiResourceBase { abstract class ApiResource extends ApiResourceBase { - async retrieve(id: string | ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async retrieve(id: string | ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.retrieve((typeof id === 'string') ? { type: this.type(), id } : id, params, options) } - async list(params?: QueryParamsList, options?: ResourcesConfig): Promise> { + async list(params?: QueryParamsList, options?: ResourcesConfig): Promise> { return this.resources.list({ type: this.type() }, params, options) } - async count(filter?: QueryFilter | QueryParamsList, options?: ResourcesConfig): Promise { - const params: QueryParamsList = { filters: isParamsList(filter) ? filter.filters : filter, pageNumber: 1, pageSize: 1 } + async count(filter?: QueryFilter | QueryParamsList, options?: ResourcesConfig): Promise { + const params: QueryParamsList = { filters: isParamsList(filter) ? filter.filters : filter, pageNumber: 1, pageSize: 1 } const response = await this.list(params, options) return Promise.resolve(response.meta.recordCount) } @@ -336,7 +336,7 @@ abstract class ApiResource extends ApiResourceBase { abstract class ApiSingleton extends ApiResourceBase { - async retrieve(params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async retrieve(params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.retrieve({ type: this.type() }, params, options) } diff --git a/src/resources/api_credentials.ts b/src/resources/api_credentials.ts index b89a336..7cf0f4f 100644 --- a/src/resources/api_credentials.ts +++ b/src/resources/api_credentials.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiResource } from '../resource' import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -142,11 +143,11 @@ class ApiCredentials extends ApiResource { static readonly TYPE: ApiCredentialType = 'api_credentials' as const - async create(resource: ApiCredentialCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async create(resource: ApiCredentialCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.create({ ...resource, type: ApiCredentials.TYPE }, params, options) } - async update(resource: ApiCredentialUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: ApiCredentialUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: ApiCredentials.TYPE }, params, options) } diff --git a/src/resources/application_memberships.ts b/src/resources/application_memberships.ts index 215b0f5..1a1c736 100644 --- a/src/resources/application_memberships.ts +++ b/src/resources/application_memberships.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiResource } from '../resource' import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -72,11 +73,11 @@ class ApplicationMemberships extends ApiResource { static readonly TYPE: ApplicationMembershipType = 'application_memberships' as const - async create(resource: ApplicationMembershipCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async create(resource: ApplicationMembershipCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.create({ ...resource, type: ApplicationMemberships.TYPE }, params, options) } - async update(resource: ApplicationMembershipUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: ApplicationMembershipUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: ApplicationMemberships.TYPE }, params, options) } diff --git a/src/resources/memberships.ts b/src/resources/memberships.ts index d920b51..2761a54 100644 --- a/src/resources/memberships.ts +++ b/src/resources/memberships.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiResource } from '../resource' import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ListResponse, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -85,11 +86,11 @@ class Memberships extends ApiResource { static readonly TYPE: MembershipType = 'memberships' as const - async create(resource: MembershipCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async create(resource: MembershipCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.create({ ...resource, type: Memberships.TYPE }, params, options) } - async update(resource: MembershipUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: MembershipUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: Memberships.TYPE }, params, options) } diff --git a/src/resources/organizations.ts b/src/resources/organizations.ts index daf9e97..d798c52 100644 --- a/src/resources/organizations.ts +++ b/src/resources/organizations.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiResource } from '../resource' import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ListResponse, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -271,11 +272,11 @@ class Organizations extends ApiResource { static readonly TYPE: OrganizationType = 'organizations' as const - async create(resource: OrganizationCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async create(resource: OrganizationCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.create({ ...resource, type: Organizations.TYPE }, params, options) } - async update(resource: OrganizationUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: OrganizationUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: Organizations.TYPE }, params, options) } diff --git a/src/resources/permissions.ts b/src/resources/permissions.ts index edc5550..55fd876 100644 --- a/src/resources/permissions.ts +++ b/src/resources/permissions.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiResource } from '../resource' import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ListResponse, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -108,11 +109,11 @@ class Permissions extends ApiResource { static readonly TYPE: PermissionType = 'permissions' as const - async create(resource: PermissionCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async create(resource: PermissionCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.create({ ...resource, type: Permissions.TYPE }, params, options) } - async update(resource: PermissionUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: PermissionUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: Permissions.TYPE }, params, options) } diff --git a/src/resources/roles.ts b/src/resources/roles.ts index 34e2caa..68a08c6 100644 --- a/src/resources/roles.ts +++ b/src/resources/roles.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiResource } from '../resource' import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ListResponse, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -71,11 +72,11 @@ class Roles extends ApiResource { static readonly TYPE: RoleType = 'roles' as const - async create(resource: RoleCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async create(resource: RoleCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.create({ ...resource, type: Roles.TYPE }, params, options) } - async update(resource: RoleUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: RoleUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { return this.resources.update({ ...resource, type: Roles.TYPE }, params, options) } diff --git a/src/resources/user.ts b/src/resources/user.ts index b1fe52b..75c2a73 100644 --- a/src/resources/user.ts +++ b/src/resources/user.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars import type { Nullable } from '../types' import { ApiSingleton } from '../resource' import type { Resource, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ResourceSort, /* ResourceFilter */ } from '../resource' @@ -75,7 +76,7 @@ class Users extends ApiSingleton { static readonly TYPE: UserType = 'user' as const - async update(resource: UserUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { + async update(resource: UserUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise { const res = await this.retrieve(params, options) // JsonAPI requires id in the request body return this.resources.update({ ...resource, id: res.id, type: Users.TYPE }, params, options) } diff --git a/src/util.ts b/src/util.ts index d6a0553..246d63d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,4 @@ -import type { ObjectType } from "../src/common" +import type { ObjectType } from "../src/types" // import path from 'path'