Skip to content

Commit

Permalink
add validation of generated types to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
wernst authored and pbohlman committed Jun 14, 2024
1 parent 0e4d5d2 commit 65f1d4b
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 26 deletions.
5 changes: 3 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"version": "0.3.68",
"type": "module",
"scripts": {
"build": "tsc",
"build": "tsc && yarn validate:types",
"build:watch": "tsc -w",
"lint": "tsc --noEmit",
"lint:strict": "tsc --noEmit --strict",
"test": "vitest run",
"test:watch": "vitest watch",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js"
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"bin": {
"triplit": "./dist/index.js"
Expand Down
5 changes: 3 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@
}
},
"scripts": {
"build": "tsc && yarn build-worker",
"build": "tsc && yarn validate:types && yarn build-worker",
"build:watch": "tsc -w",
"lint:build": "npx publint",
"lint": "tsc --noEmit",
"test": "yarn test:unit && yarn typecheck",
"test:unit": "vitest run",
"typecheck": "vitest --typecheck.only --no-watch",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js",
"build-worker": "esbuild ./src/worker-client/worker-client-operator.ts --bundle --minify --platform=browser --outfile=./dist/worker-client/worker-client-operator.js --format=esm --sourcemap"
"build-worker": "esbuild ./src/worker-client/worker-client-operator.ts --bundle --minify --platform=browser --outfile=./dist/worker-client/worker-client-operator.js --format=esm --sourcemap",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"files": [
"/dist"
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/client/triplit-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TupleValue,
schemaToJSON,
Unalias,
SchemaJSON,
} from '@triplit/db';
import { getUserId } from '../token.js';
import { UnrecognizedFetchPolicyError } from '../errors.js';
Expand Down Expand Up @@ -260,7 +261,7 @@ export class TriplitClient<M extends ClientSchema | undefined = undefined> {
});
}

async getSchema() {
async getSchema(): Promise<SchemaJSON | undefined> {
return schemaToJSON(await this.db.getSchema());
}

Expand Down
5 changes: 3 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"type": "module",
"scripts": {
"build": "tsc --build --pretty",
"build": "tsc --build --pretty && yarn validate:types",
"build:watch": "tsc -w",
"lint:build": "npx publint",
"lint": "tsc --noEmit",
Expand All @@ -46,7 +46,8 @@
"typecheck": "vitest --typecheck.only --no-watch",
"bench": "vitest bench",
"coverage": "stryker run",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js"
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"files": [
"/dist"
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export type {
CollectionsDefinition,
QueryAttributeDefinition,
UserTypeOptions,
SchemaDefinition as SchemaJSON,
} from './data-types/serialization.js';
export { timestampCompare } from './timestamp.js';
export type { Timestamp } from './timestamp.js';
Expand Down
5 changes: 3 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
}
},
"scripts": {
"build": "tsc",
"build": "tsc && yarn validate:types",
"build:watch": "tsc -w",
"lint:build": "npx publint",
"lint": "tsc --noEmit",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js"
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"files": [
"/dist"
Expand Down
52 changes: 43 additions & 9 deletions packages/react/src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,54 @@ import {
} from '@triplit/client';
import type { WorkerClient } from '@triplit/client/worker-client';

export function useQuery<
type useQueryPayload<
M extends Models<any, any> | undefined,
Q extends ClientQuery<M, any, any, any>
>(
client: TriplitClient<M> | WorkerClient<M>,
query: ClientQueryBuilder<Q>,
options?: Partial<SubscriptionOptions>
): {
> = {
results: Unalias<ClientFetchResult<Q>> | undefined;
fetching: boolean;
fetchingLocal: boolean;
fetchingRemote: boolean;
error: any;
};

type usePaginatedQueryPayload<
M extends Models<any, any> | undefined,
Q extends ClientQuery<M, any, any, any>
> = {
results: Unalias<ClientFetchResult<Q>> | undefined;
fetching: boolean;
fetchingPage: boolean;
error: any;
hasNextPage: boolean;
hasPreviousPage: boolean;
nextPage: () => void;
prevPage: () => void;
disconnect: () => void;
};

type useInfiniteQueryPayload<
M extends Models<any, any> | undefined,
Q extends ClientQuery<M, any, any, any>
> = {
results: Unalias<ClientFetchResult<Q>> | undefined;
fetching: boolean;
fetchingRemote: boolean;
fetchingMore: boolean;
error: any;
} {
hasMore: boolean;
loadMore: () => void;
disconnect: () => void;
};

export function useQuery<
M extends Models<any, any> | undefined,
Q extends ClientQuery<M, any, any, any>
>(
client: TriplitClient<M> | WorkerClient<M>,
query: ClientQueryBuilder<Q>,
options?: Partial<SubscriptionOptions>
): useQueryPayload<M, Q> {
const [results, setResults] = useState<
Unalias<ClientFetchResult<Q>> | undefined
>(undefined);
Expand Down Expand Up @@ -103,7 +137,7 @@ export function usePaginatedQuery<
client: TriplitClient<M> | WorkerClient<M>,
query: ClientQueryBuilder<Q>,
options?: Partial<SubscriptionOptions>
) {
): usePaginatedQueryPayload<M, Q> {
const builtQuery = useMemo(() => query.build(), [query]);
const [hasNextPage, setHasNextPage] = useState(false);
const [hasPreviousPage, setHasPreviousPage] = useState(false);
Expand Down Expand Up @@ -178,7 +212,7 @@ export function useInfiniteQuery<
client: TriplitClient<M> | WorkerClient<M>,
query: ClientQueryBuilder<Q>,
options?: Partial<SubscriptionOptions>
) {
): useInfiniteQueryPayload<M, Q> {
const builtQuery = useMemo(() => query.build(), [query]);
const stringifiedQuery = builtQuery && JSON.stringify(builtQuery);
const [hasMore, setHasMore] = useState(false);
Expand Down
5 changes: 3 additions & 2 deletions packages/server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@
}
},
"scripts": {
"build": "tsc",
"build": "tsc && yarn validate:types",
"build:watch": "tsc -w",
"test": "vitest",
"lint": "tsc --noEmit",
"lint:build": "npx publint",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js"
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"dependencies": {
"@triplit/db": "workspace:^",
Expand Down
5 changes: 3 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
"dist"
],
"scripts": {
"build": "tsc",
"build": "tsc && yarn validate:types",
"build:watch": "tsc -w",
"lint": "tsc --noEmit",
"lint:build": "npx publint",
"util:gentoken": "node ./dev/scripts/createToken.cjs",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js && yarn sentry:sourcemaps",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org aspen --project triplit-server ./dist && sentry-cli sourcemaps upload --release=$npm_package_version --org aspen --project triplit-server ./dist"
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org aspen --project triplit-server ./dist && sentry-cli sourcemaps upload --release=$npm_package_version --org aspen --project triplit-server ./dist",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"dependencies": {
"@sentry/cli": "^2.31.2",
Expand Down
5 changes: 3 additions & 2 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
}
},
"scripts": {
"build": "svelte-package -t=false && tsc",
"build": "svelte-package -t=false && tsc && yarn validate:types",
"build:watch": "svelte-package -w",
"lint:build": "npx publint",
"publish-pkg": "node ../../scripts/npm-check-version-and-publish.js",
"check": "svelte-check --tsconfig ./tsconfig.json"
"check": "svelte-check --tsconfig ./tsconfig.json",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"files": [
"/dist"
Expand Down
5 changes: 3 additions & 2 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "@triplit/types",
"packageManager": "[email protected]",
"scripts": {
"build": "tsc",
"build:watch": "tsc -w"
"build": "tsc && yarn validate:types",
"build:watch": "tsc -w",
"validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist"
},
"exports": {
"./*": {
Expand Down
69 changes: 69 additions & 0 deletions scripts/check-for-local-references-in-declarations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const fs = require('fs');
const path = require('path');

// Function to recursively search through directories
function searchInDirectory(dir, searchString) {
let stringFound = false;
let files;
try {
files = fs.readdirSync(dir);
} catch (e) {
console.error(`Unable to scan directory: ${e}`);
throw e;
}

for (const file of files) {
const filePath = path.join(dir, file);
try {
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
stringFound = stringFound || searchInDirectory(filePath, searchString); // Recursively search in subdirectory
} else if (filePath.endsWith('.d.ts')) {
stringFound = stringFound || searchInFile(filePath, searchString);
}
} catch (e) {
console.error(`Unable to get stats of file: ${e}`);
throw e;
}
}

return stringFound;
}

// Function to search for the string in a file
function searchInFile(file, searchString) {
try {
const data = fs.readFileSync(file, 'utf8');
if (data.includes(searchString)) {
console.log(`Found "${searchString}" in file: ${file}`);
return true;
}
return false;
} catch (e) {
console.error(`Unable to read file: ${e}`);
throw e;
}
}

// Main function
function main() {
const args = process.argv.slice(2);
if (args.length !== 1) {
console.error('invalid args');
return;
}

const directory = args[0];
const localImportPrefix = 'import("packages';

const isMatch = searchInDirectory(directory, localImportPrefix);
if (isMatch) {
console.log(
'Found local references in declarations. Please remove them before publishing.'
);
process.exit(1);
}
}

// Run the main function
main();

0 comments on commit 65f1d4b

Please sign in to comment.