Skip to content

Commit

Permalink
chore(react,vue): use shared types (#97)
Browse files Browse the repository at this point in the history
* chore(react,vue): bit of housekeeping, extracting out common types

* chore(react): using common types in react package

* fix(vue): void result of renderSsr

* chore(react): cleaning up imports

* chore(vue): migrate to shared types

* chore(vue,react): update vite config
  • Loading branch information
nmathew98 committed Mar 3, 2024
1 parent 60fa805 commit 2494a67
Show file tree
Hide file tree
Showing 43 changed files with 216 additions and 186 deletions.
3 changes: 2 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"clean": "rm *.tgz && rm -rf dist",
"clean": "rm -f *.tgz && rm -rf dist",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"format": "prettier . --write",
"preview": "vite preview",
Expand All @@ -53,6 +53,7 @@
},
"dependencies": {
"@b.s/incremental": "^2.1.4",
"@b.s/qwery-shared": "link:/Users/naveenmathew/Developer/skulpture/@b.s/qwery/packages/shared",
"@b.s/txn": "^1.0.4"
},
"peerDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/e2e/use-qwery.csr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { describe, it, expect, vitest, afterEach } from "vitest";
import { QweryContext, QweryProvider, makeMonitoredFetch, useQwery } from "..";
import { QweryContext, QweryProvider } from "../context";
import { useQwery } from "../use-qwery";
import {
render,
screen,
Expand All @@ -10,6 +11,7 @@ import {
} from "@testing-library/react";
import { createApi } from "./api";
import { createRedisCache } from "./redis";
import { makeMonitoredFetch } from "@b.s/incremental";

describe("useQwery csr", () => {
const BrowserProviders = ({ children }) => (
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/e2e/use-qwery.ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import ReactDOMServer from "react-dom/server";
import { describe, it, expect, vitest, afterEach } from "vitest";
import {
ExecutionEnvironment,
QweryProvider,
useExecutionEnvironment,
useQwery,
} from "..";
} from "../use-execution-environment";
import { QweryProvider } from "../context";
import { useQwery } from "../use-qwery";
import { createRedisCache } from "./redis";
import {
render,
Expand Down Expand Up @@ -194,5 +194,5 @@ export const renderSsr = (ui: React.ReactNode) => {
document.body.appendChild(container);
container.innerHTML = serverRendered;

return render(ui, { hydrate: true, container });
return void render(ui, { hydrate: true, container });
};
3 changes: 2 additions & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export {
type CacheStore,
} from "@b.s/incremental";

export * from "@b.s/qwery-shared";

export * from "./context";
export * from "./context/types";
export * from "./use-qwery";
export * from "./use-qwery/types";
export * from "./use-remember-scroll";
export * from "./use-execution-environment";
7 changes: 3 additions & 4 deletions packages/react/src/use-execution-environment/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export enum ExecutionEnvironment {
Browser = 1,
Server,
}
import { ExecutionEnvironment } from "@b.s/qwery-shared";

export { ExecutionEnvironment } from "@b.s/qwery-shared";

export const useExecutionEnvironment = () => {
const isBrowser = Boolean(
Expand Down
9 changes: 3 additions & 6 deletions packages/react/src/use-network-mode/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React from "react";

export enum NetworkMode {
Offline = 0,
Online,
}
import { NetworkMode, UseNetworkModeOptions } from "@b.s/qwery-shared";
export { NetworkMode } from "@b.s/qwery-shared";

export const useNetworkMode = ({
ping = "https://captive.apple.com/hotspot-detect.html",
}) => {
}: UseNetworkModeOptions) => {
const [isConnected, setIsConnected] = React.useState(true);
const isOnline = () => setIsConnected(true);
const isOffline = () => setIsConnected(false);
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/use-qwery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
InitialValue,
UseQweryOptions,
UseQweryReturnWithSuspense,
} from "./types";
} from "@b.s/qwery-shared";

export const useQwery = <
I extends InitialValue,
Expand Down
6 changes: 2 additions & 4 deletions packages/react/src/use-remember-scroll/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from "react";
import {
ExecutionEnvironment,
useExecutionEnvironment,
} from "../use-execution-environment";
import { ExecutionEnvironment } from "@b.s/qwery-shared";
import { useExecutionEnvironment } from "../use-execution-environment";

export const useRememberScroll = () => {
const { executionEnvironment } = useExecutionEnvironment();
Expand Down
3 changes: 1 addition & 2 deletions packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false
},
"include": ["react/src"],
"exclude": ["react/src/e2e"],
"include": ["src/**/*"],
"references": [{ "path": "./tsconfig.node.json" }]
}
2 changes: 1 addition & 1 deletion packages/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default defineConfig({
react(),
dts({
rollupTypes: true,
include: resolve("src"),
logLevel: "info",
bundledPackages: ["@b.s/qwery-shared"],
}),
],
build: {
Expand Down
21 changes: 21 additions & 0 deletions packages/shared/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
plugins: ["prettier"],
rules: {
"prettier/prettier": ["error", { usePrettierrc: true }],
"@typescript-eslint/no-explicit-any": 0,
eqeqeq: ["error", "always"],
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_" },
],
},
};
26 changes: 26 additions & 0 deletions packages/shared/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

*.tgz
3 changes: 3 additions & 0 deletions packages/shared/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/**
dist/**
pnpm-lock.yaml
14 changes: 14 additions & 0 deletions packages/shared/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"tabWidth": 4,
"useTabs": true,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "avoid",
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto",
"printWidth": 80
}
21 changes: 21 additions & 0 deletions packages/shared/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@b.s/qwery-shared",
"version": "1.4.2",
"private": true,
"type": "module",
"devDependencies": {
"@b.s/incremental": "^2.1.4",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.0.3",
"typescript": "~5.3.0"
},
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"clean": "rm -f *.tgz && rm -rf dist",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"format": "prettier . --write"
}
}
3 changes: 3 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./use-execution-environment";
export * from "./use-network-mode";
export * from "./use-qwery";
4 changes: 4 additions & 0 deletions packages/shared/src/use-execution-environment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum ExecutionEnvironment {
Browser = 1,
Server,
}
6 changes: 6 additions & 0 deletions packages/shared/src/use-network-mode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./types";

export enum NetworkMode {
Offline = 0,
Online,
}
1 change: 1 addition & 0 deletions packages/shared/src/use-qwery/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./types";
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "Bundler",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,

"outDir": "dist",
"declaration": true
},
"include": ["src/**/*"]
}
3 changes: 2 additions & 1 deletion packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"clean": "rm *.tgz && rm -rf dist",
"clean": "rm -f *.tgz && rm -rf dist",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"format": "prettier . --write",
"preview": "vite preview",
Expand All @@ -53,6 +53,7 @@
},
"dependencies": {
"@b.s/incremental": "^2.1.4",
"@b.s/qwery-shared": "link:/Users/naveenmathew/Developer/skulpture/@b.s/qwery/packages/shared",
"@b.s/txn": "^1.0.4"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/e2e/use-qwery.csr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe("useQwery csr", () => {
<div>c: {{ test.data.value?.c }}</div>
`,
setup: () => {
const test = useQwery<{ a: number; b: number; c: number }>({
const test = useQwery({
initialValue: getInitialValue,
onChange: vitest.fn(),
refetchOnWindowFocus: true,
Expand Down
1 change: 1 addition & 0 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from "./use-network-mode";
export * from "./context";
export * from "./use-remember-scroll";
export * from "./use-qwery";
export * from "@b.s/qwery-shared";
7 changes: 3 additions & 4 deletions packages/vue/src/use-execution-environment/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export enum ExecutionEnvironment {
Browser = 1,
Server,
}
import { ExecutionEnvironment } from "@b.s/qwery-shared";

export { ExecutionEnvironment } from "@b.s/qwery-shared";

export const useExecutionEnvironment = () => {
const isBrowser = Boolean(
Expand Down
8 changes: 3 additions & 5 deletions packages/vue/src/use-network-mode/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { computed, onMounted, onUnmounted, onUpdated, ref } from "vue";
import { NetworkMode, type UseNetworkModeOptions } from "@b.s/qwery-shared";

export enum NetworkMode {
Offline = 0,
Online,
}
export { NetworkMode, type UseNetworkModeOptions } from "@b.s/qwery-shared";

export const useNetworkMode = ({
ping = "https://captive.apple.com/hotspot-detect.html",
}) => {
}: UseNetworkModeOptions) => {
const isConnected = ref(true);

const isOnline = () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/vue/src/use-network-mode/types.ts

This file was deleted.

8 changes: 2 additions & 6 deletions packages/vue/src/use-qwery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { computed, onMounted, onUnmounted, shallowReactive } from "vue";
import { createCRDT, type CRDT, type Dispatch } from "@b.s/incremental";
import { useQweryContext } from "../context";
import { useRememberScroll } from "../use-remember-scroll";
import type {
Data,
InitialValue,
UseQweryOptions,
UseQweryReturnWithSuspense,
} from "./types";
import type { Data, InitialValue, UseQweryOptions } from "@b.s/qwery-shared";
import type { UseQweryReturnWithSuspense } from "./types";

export const useQwery = <
I extends InitialValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Dispatch } from "@b.s/incremental";
import type { Data, InitialValue } from "./options";
import type {
UseQweryReturn as SharedUseQweryReturn,
InitialValue,
Data,
} from "@b.s/qwery-shared";
import type { ComputedRef } from "vue";

export interface UseQweryReturn<
Expand All @@ -9,10 +12,9 @@ export interface UseQweryReturn<
>
? DInferred
: I,
> {
> extends Pick<SharedUseQweryReturn<I, DInferred>, "dispatch"> {
data: ComputedRef<DInferred | undefined>;
versions: ComputedRef<DInferred[] | undefined>;
dispatch: Dispatch<DInferred>;
}

export type UseQweryReturnWithSuspense<
Expand Down
Loading

0 comments on commit 2494a67

Please sign in to comment.