Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to new style imports. #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fix-commonjs-dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as fs from 'fs'

fs.writeFileSync('./dist/main/package.json', '{"type": "commonjs"}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node looks at type in the nearest package.json to tell if a .jsfile should be interpreted as an ESM or commonjs module. You can use .mjs or .cjs to specify this on a per-file basis, but typescript doesn't have any support for that. See https://github.com/azu/tsconfig-to-dual-package#motivation for more details and links to discussion about this.

I elected to put type: module in the top-level package, since my impression is that the ecosystem is slowly moving towards ESM style modules.

5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
export default {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from https://kulshekhar.github.io/ts-jest/docs/guides/esm-support, but it isn't clear to me why this isn't included in any of the presets.

}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
@@ -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<Result>
implements PromiseLike<PostgrestSingleResponse<Result>>
Expand Down
10 changes: 5 additions & 5 deletions src/PostgrestClient.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
8 changes: 4 additions & 4 deletions src/PostgrestQueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { version } from './version'
import { version } from './version.js'
export const DEFAULT_HEADERS = { 'X-Client-Info': `postgrest-js/${version}` }
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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,
PostgrestResponseSuccess,
PostgrestSingleResponse,
PostgrestMaybeSingleResponse,
PostgrestError,
} from './types'
} from './types.js'
2 changes: 1 addition & 1 deletion src/select-query-parser.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
4 changes: 2 additions & 2 deletions test/basic.ts
Original file line number Diff line number Diff line change
@@ -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<Database>(REST_URL)
Expand Down
4 changes: 2 additions & 2 deletions test/filters.ts
Original file line number Diff line number Diff line change
@@ -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<Database>('http://localhost:3000')

Expand Down
4 changes: 2 additions & 2 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Database>(REST_URL)
Expand Down
4 changes: 2 additions & 2 deletions test/resource-embedding.ts
Original file line number Diff line number Diff line change
@@ -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<Database>('http://localhost:3000')

Expand Down
4 changes: 2 additions & 2 deletions test/transforms.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down