diff --git a/fix-commonjs-dist.js b/fix-commonjs-dist.js new file mode 100644 index 00000000..6b4fabb7 --- /dev/null +++ b/fix-commonjs-dist.js @@ -0,0 +1,3 @@ +import * as fs from 'fs' + +fs.writeFileSync('./dist/main/package.json', '{"type": "commonjs"}') diff --git a/jest.config.js b/jest.config.js index 87c30aa4..c095e87c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,4 +1,7 @@ -module.exports = { +export default { preset: 'ts-jest', testEnvironment: 'node', + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, } diff --git a/package.json b/package.json index 0e051492..0005471a 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,21 @@ "dist", "src" ], + "type": "module", "main": "dist/main/index.js", "module": "dist/module/index.js", "types": "dist/module/index.d.ts", + "exports": { + "require": "./dist/main/index.js", + "import": "./dist/module/index.js", + "types": "./dist/module/index.d.ts" + }, "repository": "supabase/postgrest-js", "scripts": { "clean": "rimraf dist docs/v2", "format": "prettier --write \"{src,test}/**/*.ts\"", "build": "run-s clean format build:*", - "build:main": "tsc -p tsconfig.json", + "build:main": "tsc -p tsconfig.json && node ./fix-commonjs-dist.js", "build:module": "tsc -p tsconfig.module.json", "docs": "typedoc src/index.ts --out docs/v2", "docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts", diff --git a/src/PostgrestBuilder.ts b/src/PostgrestBuilder.ts index 78d7c214..8afde705 100644 --- a/src/PostgrestBuilder.ts +++ b/src/PostgrestBuilder.ts @@ -1,6 +1,6 @@ import crossFetch from 'cross-fetch' -import type { Fetch, PostgrestSingleResponse } from './types' +import type { Fetch, PostgrestSingleResponse } from './types.js' export default abstract class PostgrestBuilder implements PromiseLike> diff --git a/src/PostgrestClient.ts b/src/PostgrestClient.ts index 9e331104..15811e23 100644 --- a/src/PostgrestClient.ts +++ b/src/PostgrestClient.ts @@ -1,8 +1,8 @@ -import PostgrestQueryBuilder from './PostgrestQueryBuilder' -import PostgrestFilterBuilder from './PostgrestFilterBuilder' -import PostgrestBuilder from './PostgrestBuilder' -import { DEFAULT_HEADERS } from './constants' -import { Fetch, GenericSchema } from './types' +import PostgrestQueryBuilder from './PostgrestQueryBuilder.js' +import PostgrestFilterBuilder from './PostgrestFilterBuilder.js' +import PostgrestBuilder from './PostgrestBuilder.js' +import { DEFAULT_HEADERS } from './constants.js' +import { Fetch, GenericSchema } from './types.js' /** * PostgREST client. diff --git a/src/PostgrestFilterBuilder.ts b/src/PostgrestFilterBuilder.ts index 1e227786..dabb24f6 100644 --- a/src/PostgrestFilterBuilder.ts +++ b/src/PostgrestFilterBuilder.ts @@ -1,5 +1,5 @@ -import PostgrestTransformBuilder from './PostgrestTransformBuilder' -import { GenericSchema } from './types' +import PostgrestTransformBuilder from './PostgrestTransformBuilder.js' +import { GenericSchema } from './types.js' type FilterOperator = | 'eq' diff --git a/src/PostgrestQueryBuilder.ts b/src/PostgrestQueryBuilder.ts index 53c3d461..87112615 100644 --- a/src/PostgrestQueryBuilder.ts +++ b/src/PostgrestQueryBuilder.ts @@ -1,7 +1,7 @@ -import PostgrestBuilder from './PostgrestBuilder' -import PostgrestFilterBuilder from './PostgrestFilterBuilder' -import { GetResult } from './select-query-parser' -import { Fetch, GenericSchema, GenericTable, GenericView } from './types' +import PostgrestBuilder from './PostgrestBuilder.js' +import PostgrestFilterBuilder from './PostgrestFilterBuilder.js' +import { GetResult } from './select-query-parser.js' +import { Fetch, GenericSchema, GenericTable, GenericView } from './types.js' export default class PostgrestQueryBuilder< Schema extends GenericSchema, diff --git a/src/PostgrestTransformBuilder.ts b/src/PostgrestTransformBuilder.ts index 646e85ab..27c70441 100644 --- a/src/PostgrestTransformBuilder.ts +++ b/src/PostgrestTransformBuilder.ts @@ -1,6 +1,6 @@ -import PostgrestBuilder from './PostgrestBuilder' -import { GetResult } from './select-query-parser' -import { GenericSchema } from './types' +import PostgrestBuilder from './PostgrestBuilder.js' +import { GetResult } from './select-query-parser.js' +import { GenericSchema } from './types.js' export default class PostgrestTransformBuilder< Schema extends GenericSchema, diff --git a/src/constants.ts b/src/constants.ts index 9870d641..c879662e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,2 +1,2 @@ -import { version } from './version' +import { version } from './version.js' export const DEFAULT_HEADERS = { 'X-Client-Info': `postgrest-js/${version}` } diff --git a/src/index.ts b/src/index.ts index 9fb4cb78..2835ec1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ -export { default as PostgrestClient } from './PostgrestClient' -export { default as PostgrestQueryBuilder } from './PostgrestQueryBuilder' -export { default as PostgrestFilterBuilder } from './PostgrestFilterBuilder' -export { default as PostgrestTransformBuilder } from './PostgrestTransformBuilder' -export { default as PostgrestBuilder } from './PostgrestBuilder' +export { default as PostgrestClient } from './PostgrestClient.js' +export { default as PostgrestQueryBuilder } from './PostgrestQueryBuilder.js' +export { default as PostgrestFilterBuilder } from './PostgrestFilterBuilder.js' +export { default as PostgrestTransformBuilder } from './PostgrestTransformBuilder.js' +export { default as PostgrestBuilder } from './PostgrestBuilder.js' export { PostgrestResponse, PostgrestResponseFailure, @@ -10,4 +10,4 @@ export { PostgrestSingleResponse, PostgrestMaybeSingleResponse, PostgrestError, -} from './types' +} from './types.js' diff --git a/src/select-query-parser.ts b/src/select-query-parser.ts index 5f8c5a74..53b5fa78 100644 --- a/src/select-query-parser.ts +++ b/src/select-query-parser.ts @@ -1,6 +1,6 @@ // Credits to @bnjmnt4n (https://www.npmjs.com/package/postgrest-query) -import { GenericSchema, Prettify } from './types' +import { GenericSchema, Prettify } from './types.js' type Whitespace = ' ' | '\n' | '\t' diff --git a/test/basic.ts b/test/basic.ts index 15059ec4..f7b1e03a 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -1,5 +1,5 @@ -import { PostgrestClient } from '../src/index' -import { Database } from './types' +import { PostgrestClient } from '../src/index.js' +import { Database } from './types.js' const REST_URL = 'http://localhost:3000' const postgrest = new PostgrestClient(REST_URL) diff --git a/test/filters.ts b/test/filters.ts index 5ec35b86..7b34e72a 100644 --- a/test/filters.ts +++ b/test/filters.ts @@ -1,5 +1,5 @@ -import { PostgrestClient } from '../src/index' -import { Database } from './types' +import { PostgrestClient } from '../src/index.js' +import { Database } from './types.js' const postgrest = new PostgrestClient('http://localhost:3000') diff --git a/test/index.test-d.ts b/test/index.test-d.ts index b6d388d0..331c3bf3 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -1,6 +1,6 @@ import { expectError, expectType } from 'tsd' -import { PostgrestClient } from '../src/index' -import { Database, Json } from './types' +import { PostgrestClient } from '../src/index.js' +import { Database, Json } from './types.js' const REST_URL = 'http://localhost:3000' const postgrest = new PostgrestClient(REST_URL) diff --git a/test/resource-embedding.ts b/test/resource-embedding.ts index 08887be0..2c0d6e92 100644 --- a/test/resource-embedding.ts +++ b/test/resource-embedding.ts @@ -1,5 +1,5 @@ -import { PostgrestClient } from '../src/index' -import { Database } from './types' +import { PostgrestClient } from '../src/index.js' +import { Database } from './types.js' const postgrest = new PostgrestClient('http://localhost:3000') diff --git a/test/transforms.ts b/test/transforms.ts index 8d82fa66..a3ef0d0a 100644 --- a/test/transforms.ts +++ b/test/transforms.ts @@ -1,5 +1,5 @@ -import { PostgrestClient } from '../src/index' -import { Database } from './types' +import { PostgrestClient } from '../src/index.js' +import { Database } from './types.js' import { AbortController } from 'node-abort-controller'