diff --git a/package.json b/package.json index fc6aa74..b8bbf33 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "pinejs-client-supertest", "version": "2.1.4", "description": "This module provides the nodejs interface for the pinejs API using request.", + "type": "commonjs", "main": "build/index.js", "types": "build/index.d.ts", "repository": { @@ -23,8 +24,8 @@ "build/" ], "engines": { - "node": ">=16.13.0", - "npm": ">=8.0.0" + "node": ">=20.0.0", + "npm": ">=10.0.0" }, "scripts": { "build": "npm run lint && tsc", @@ -41,14 +42,14 @@ "typescript": "^5.6.2" }, "dependencies": { - "@balena/abstract-sql-to-typescript": "^3.3.1", + "@balena/abstract-sql-to-typescript": "^4.0.6", "@types/chai": "^4.3.19", "@types/chai-as-promised": "^7.1.8", "@types/express": "^4.17.21", "@types/supertest": "^6.0.2", "chai": "^4.5.0", "chai-as-promised": "^7.1.2", - "pinejs-client-core": "^6.15.11", + "pinejs-client-core": "^8.0.1", "supertest": "^7.0.0" }, "versionist": { diff --git a/src/chai.ts b/src/chai.ts index 943f9b7..deb74bd 100644 --- a/src/chai.ts +++ b/src/chai.ts @@ -1,5 +1,5 @@ import { use } from 'chai'; -import * as chaiAsPromised from 'chai-as-promised'; +import chaiAsPromised from 'chai-as-promised'; use(chaiAsPromised); export { expect } from 'chai'; diff --git a/src/index.ts b/src/index.ts index e8fec8e..41c425d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,9 +2,8 @@ import type { AnyObject, AnyResource, ConstructorParams, - ODataOptions, + OptionsToResponse, Params, - PromiseResultTypes, Resource, RetryParameters, } from 'pinejs-client-core'; @@ -16,82 +15,7 @@ import { expect } from './chai'; import type { UserParam, Overwrite } from './common'; import type { PickDeferred } from '@balena/abstract-sql-to-typescript'; -type Expanded = Extract>; type StringKeyOf = keyof T & string; -type SelectPropsOf> = - U['$select'] extends ReadonlyArray> - ? U['$select'][number] - : U['$select'] extends StringKeyOf - ? U['$select'] - : // If no $select is provided, all properties that are not $expanded are selected - Exclude, ExpandPropsOf>; -type ExpandPropsOf< - T extends Resource['Read'], - U extends ODataOptions, -> = U['$expand'] extends { [key in StringKeyOf]?: any } - ? StringKeyOf - : U['$expand'] extends ReadonlyArray> - ? U['$expand'][number] - : // If no $expand is provided, no properties are expanded - never; -type ExpandToResponse< - T extends Resource['Read'], - U extends ODataOptions, -> = U['$expand'] extends { [key in StringKeyOf]?: any } - ? { - [P in keyof U['$expand']]-?: OptionsToResponse< - Expanded[number], - U['$expand'][P], - undefined - >; - } - : U['$expand'] extends ReadonlyArray> - ? { - [P in U['$expand'][number]]-?: Array< - PickDeferred[number]> - >; - } - : // If no $expand is provided, no properties are expanded - // eslint-disable-next-line @typescript-eslint/no-empty-object-type -- We do want an empty object but `Record` doesn't work because things breaks after it's used in a union, needs investigation - {}; - -// Check if two types are exactly equal, useful for checking against eg exactly `any` -type Equals = - (() => T extends X ? 1 : 2) extends () => T extends Y ? 1 : 2 - ? true - : false; - -type BaseResourceId = - | string - | number - | Date - | { - '@': string; - }; -type ResourceAlternateKey = { - [key in StringKeyOf]?: BaseResourceId; -}; -type ResourceId = - | BaseResourceId - | ResourceAlternateKey; - -export type OptionsToResponse< - T extends Resource['Read'], - U extends ODataOptions, - ID extends ResourceId | undefined, -> = U extends { - $count: ODataOptions['$count']; -} - ? number - : Equals extends true - ? - | (PickDeferred> & ExpandToResponse) - | undefined - : ID extends ResourceId - ? - | (PickDeferred> & ExpandToResponse) - | undefined - : Array> & ExpandToResponse>; type supportedMethod = 'get' | 'put' | 'patch' | 'post' | 'delete'; @@ -139,7 +63,7 @@ export class PineTest< } = { [key in string]: AnyResource; }, -> extends PinejsClientCore { +> extends PinejsClientCore { constructor( params: ConstructorParams, public backendParams: BackendParams, @@ -163,32 +87,18 @@ export class PineTest< > > >; - /** - * @deprecated GETing via `url` is deprecated - */ - public get( - params: { - resource?: undefined; - url: NonNullable['url']>; - } & Params, - ): PromiseResult; public get(params: Params): PromiseResult; public get(params: Params): PromiseResult { - // Use a different const, since if we just re-assign `params` - // inside the `expect(() => {})` TS forgets that it's ensured - // to be a ParamsObj and will complain. - const normalizedParams = - typeof params === 'string' ? { url: params } : params; - normalizedParams.method = 'GET'; + params.method = 'GET'; - return this.request(normalizedParams).expect((response) => { + return this.request(params).expect((response) => { const { error, body } = response; if (error) { return; } expect(() => { - const resultBody = this.transformGetResult(normalizedParams)(body); + const resultBody = this.transformGetResult(params, body); // We need to use Object.defineProperty in order to be able to set undefined // as the response body value, since superagent uses a getter which re-parses // the body whenever it is undefined. @@ -202,82 +112,34 @@ export class PineTest< public put>( params: { resource: TResource; url?: undefined } & Params, - ): PromiseResult; - /** - * @deprecated PUTing via `url` is deprecated - */ - public put( - params: { - resource?: undefined; - url: NonNullable['url']>; - } & Params, - ): PromiseResult; - public put(params: Params): PromiseResult { + ): PromiseResult { return super.put( - params as Parameters['put']>[0], - ) as PromiseResult< - ResolvableReturnType['put']> - >; + params as Parameters['put']>[0], + ) as PromiseResult['put']>>; } public patch>( params: { resource: TResource; url?: undefined } & Params, - ): PromiseResult; - /** - * @deprecated PATCHing via `url` is deprecated - */ - public patch( - params: { - resource?: undefined; - url: NonNullable['url']>; - } & Params, - ): PromiseResult; - public patch(params: Params): PromiseResult { + ): PromiseResult { return super.patch( - params as Parameters['patch']>[0], - ) as PromiseResult< - ResolvableReturnType['patch']> - >; + params as Parameters['patch']>[0], + ) as PromiseResult['patch']>>; } public post>( params: { resource: TResource } & Params, - ): PromiseResult>; - /** - * @deprecated POSTing via `url` is deprecated - */ - public post( - params: { - resource?: undefined; - url: NonNullable['url']>; - } & Params, - ): PromiseResult; - public post(params: Params): PromiseResult { + ): PromiseResult> { return super.post( - params as Parameters['post']>[0], - ) as PromiseResult< - ResolvableReturnType['post']> - >; + params as Parameters['post']>[0], + ) as PromiseResult['post']>>; } public delete>( params: { resource: TResource } & Params, - ): PromiseResult; - /** - * @deprecated DELETEing via `url` is deprecated - */ - public delete( - params: { - resource?: undefined; - url: NonNullable['url']>; - } & Params, - ): PromiseResult; - public delete(params: Params): PromiseResult { + ): PromiseResult { return super.delete( - params as Parameters['delete']>[0], - ) as PromiseResult< - ResolvableReturnType['delete']> - >; + params as Parameters['delete']>[0], + ) as PromiseResult['delete']>>; } public upsert(): never { @@ -289,7 +151,7 @@ export class PineTest< } public request( - ...args: Parameters['request']> + ...args: Parameters['request']> ): PromiseResult { return super.request(...args) as PromiseResult; } diff --git a/tsconfig.json b/tsconfig.json index 1852b98..545d2b3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "commonjs", + "module": "Node16", "outDir": "build", "noImplicitAny": true, "noUnusedParameters": true, @@ -8,7 +8,7 @@ "removeComments": true, "sourceMap": true, "strictNullChecks": true, - "target": "es2021", + "target": "es2023", "declaration": true, "skipLibCheck": true },