diff --git a/package-lock.json b/package-lock.json index 04597999f..d007b8cb6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35108,6 +35108,7 @@ "cpy-cli": "^5.0.0", "jest": "^28.1.0", "node-abort-controller": "^3.0.1", + "prettier": "^2.6.2", "ts-jest": "^28.0.3", "tstyche": "^4.3.0", "type-fest": "^4.32.0", @@ -36224,6 +36225,22 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "packages/core/postgrest-js/node_modules/prettier": { + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "packages/core/postgrest-js/node_modules/pretty-format": { "version": "28.1.3", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.3.tgz", diff --git a/packages/core/postgrest-js/package.json b/packages/core/postgrest-js/package.json index 5017ba874..2733ec7c7 100644 --- a/packages/core/postgrest-js/package.json +++ b/packages/core/postgrest-js/package.json @@ -34,6 +34,8 @@ }, "scripts": { "build": "npm run build:cjs && npm run build:esm", + "format": "node scripts/format.js", + "format:check": "node scripts/format.js check", "build:cjs": "tsc -p tsconfig.json", "build:esm": "cpy wrapper.mjs dist/esm/", "build:module": "npm run build:cjs", @@ -50,7 +52,7 @@ "type-check:test": "tsc --noEmit --project tsconfig.test.json", "db:clean": "cd test/db && docker compose down --volumes", "db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000", - "db:generate-test-types": "cd test/db && docker compose up --detach && wait-for-localhost 8080 && wait-for-localhost 3000 && curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ../types.generated.ts && node ../../scripts/update-json-type.js && cd ../../" + "db:generate-test-types": "cd test/db && docker compose up --detach && wait-for-localhost 8080 && wait-for-localhost 3000 && curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ../types.generated.ts && node ../scripts/update-json-type.js && cd ../../ && npm run format" }, "dependencies": { "@supabase/node-fetch": "2.6.15", @@ -65,6 +67,7 @@ "tstyche": "^4.3.0", "type-fest": "^4.32.0", "wait-for-localhost-cli": "^3.0.0", - "zod": "^3.25.76" + "zod": "^3.25.76", + "prettier": "^2.6.2" } } diff --git a/packages/core/postgrest-js/scripts/format.js b/packages/core/postgrest-js/scripts/format.js new file mode 100755 index 000000000..282b65365 --- /dev/null +++ b/packages/core/postgrest-js/scripts/format.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node + +const { execSync } = require('child_process') +const path = require('path') + +// Get the monorepo root directory (go up from scripts/ to postgrest-js/ to core/ to packages/ to root) +const monorepoRoot = path.resolve(__dirname, '../../../../') + +// Run prettier from the monorepo root +const command = + process.argv[2] === 'check' + ? 'npx prettier --ignore-path packages/core/postgrest-js/.gitignore --check "packages/core/postgrest-js/**/*{ts,js,mjs,json,yml,yaml}"' + : 'npx prettier --ignore-path packages/core/postgrest-js/.gitignore --write "packages/core/postgrest-js/**/*{ts,js,mjs,json,yml,yaml}"' + +try { + execSync(command, { cwd: monorepoRoot, stdio: 'inherit' }) +} catch (error) { + process.exit(error.status || 1) +} diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 73942b131..bf7397e1d 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -782,7 +782,7 @@ describe('advanced rpc', () => { test('polymorphic function with unnamed int param', async () => { const res = await postgrest.rpc( - // @ts-expect-error Argument of type '"polymorphic_function_with_unnamed_integer"' is not assignable to parameter of type '"blurb_message" | "function_returning_row" | "function_returning_set_of_rows" + // @ts-expect-error Argument of type '"polymorphic_function_with_unnamed_integer"' is not assignable to parameter of type 'polymorphic_function_with_unnamed_integer', { '': 1, diff --git a/packages/core/postgrest-js/test/embeded_functions_join.test.ts b/packages/core/postgrest-js/test/embeded_functions_join.test.ts index 465fe3514..0f15e9886 100644 --- a/packages/core/postgrest-js/test/embeded_functions_join.test.ts +++ b/packages/core/postgrest-js/test/embeded_functions_join.test.ts @@ -1229,36 +1229,19 @@ describe('embeded functions select', () => { // test select the created_ago embeded function test('select the created_ago embeded function', async () => { const res = await postgrest.from('users_audit').select('id, created_ago') - expect(res).toMatchInlineSnapshot(` - Object { - "count": null, - "data": Array [ - Object { - "created_ago": 7, - "id": 1, - }, - Object { - "created_ago": 7, - "id": 2, - }, - Object { - "created_ago": 7, - "id": 3, - }, - Object { - "created_ago": 7, - "id": 4, - }, - Object { - "created_ago": 7, - "id": 5, - }, - ], - "error": null, - "status": 200, - "statusText": "OK", - } - `) + // Don't snapshot time-based values - they change daily and cause flaky tests + expect(res.error).toBeNull() + expect(res.status).toBe(200) + expect(res.statusText).toBe('OK') + expect(res.count).toBeNull() + expect(res.data).toHaveLength(5) + // Verify structure of each record + expect(res.data?.[0]).toMatchObject({ + id: expect.any(Number), + created_ago: expect.any(Number), + }) + // Verify time-based value is reasonable + expect(res.data?.[0].created_ago).toBeGreaterThanOrEqual(0) let result: Exclude const ExpectedSchema = z.array( z.object({ @@ -1271,7 +1254,7 @@ describe('embeded functions select', () => { expectType>(true) ExpectedSchema.parse(res.data) const use_rpc_call = await postgrest.rpc('created_ago', { - // @ts-expect-error - id is not a parameter of the created_ago function + // @ts-expect-error Object literal may only specify known properties id: 1, }) expect(use_rpc_call).toMatchInlineSnapshot(` @@ -1301,24 +1284,19 @@ describe('embeded functions select', () => { // Test the days_since_event embeded function over partitioned table test('select the days_since_event embeded function over partitioned table', async () => { const res = await postgrest.from('events').select('id, days_since_event') - expect(res).toMatchInlineSnapshot(` - Object { - "count": null, - "data": Array [ - Object { - "days_since_event": 500, - "id": 1, - }, - Object { - "days_since_event": 222, - "id": 2, - }, - ], - "error": null, - "status": 200, - "statusText": "OK", - } - `) + // Don't snapshot time-based values - they change daily and cause flaky tests + expect(res.error).toBeNull() + expect(res.status).toBe(200) + expect(res.statusText).toBe('OK') + expect(res.count).toBeNull() + expect(res.data).toHaveLength(2) + // Verify structure of each record + expect(res.data?.[0]).toMatchObject({ + id: expect.any(Number), + days_since_event: expect.any(Number), + }) + // Verify time-based value is reasonable + expect(res.data?.[0].days_since_event).toBeGreaterThanOrEqual(0) let result: Exclude const ExpectedSchema = z.array( z.object({ @@ -1331,7 +1309,7 @@ describe('embeded functions select', () => { expectType>(true) ExpectedSchema.parse(res.data) const use_rpc_call = await postgrest.rpc('days_since_event', { - // @ts-expect-error - id is not a parameter of the days_since_event function + // @ts-expect-error Object literal may only specify known properties id: 1, }) expect(use_rpc_call).toMatchInlineSnapshot(`