Skip to content

Commit

Permalink
Create library
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio García committed Aug 9, 2021
0 parents commit 582c47c
Show file tree
Hide file tree
Showing 12 changed files with 1,469 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Publish to npm
on:
release:
types: [published]
jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 12
registry-url: https://registry.able.co/
scope: ableco
- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-yarn-
- run: yarn install
- run: npm run build
- run: npm publish
env:
NODE_ENV: "production"
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.log
.DS_Store
node_modules
dist
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"trailingComma": "all",
"proseWrap": "always"
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Abledev React
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@ableco/abledev-react",
"version": "0.0.1-alpha0",
"description": "",
"main": "dist/abledev-react.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "webpack"
},
"author": "Julio García",
"license": "ISC",
"dependencies": {
"react-query": "^3.19.2"
},
"peerDependencies": {
"react": ">=16"
},
"devDependencies": {
"@types/node": "^16.4.13",
"@types/react": "^17.0.16",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"ts-loader": "^9.2.5",
"ts-node": "^10.2.0",
"tslib": "^2.3.0",
"typescript": "^4.3.5",
"webpack": "^5.49.0",
"webpack-cli": "^4.7.2"
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as useMutation } from "./useMutation";
export { default as useQuery } from "./useQuery";
export { default as wrapRootComponent } from "./wrapRootComponent";
30 changes: 30 additions & 0 deletions src/useMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useMutation as useRQMutation } from "react-query";

type AnyFunction = (...args: any) => any;

function useMutation<MutationKey extends AnyFunction>(
mutationKey: MutationKey,
) {
// This is not a arbitrary convertion. queryKey works differently between the TS world
// and the runtime world:
// - In the TS world, this is a function
// - In the runtime world, this is a string
const mutationKeyAsString = mutationKey as unknown as string;

return useRQMutation(mutationKeyAsString, async (args: object = {}) => {
const url = new URL(`${location.origin}/abledev/call-mutation`);
url.search = new URLSearchParams({ key: mutationKeyAsString }).toString();
return fetch(url.toString(), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(args),
}).then((response) => {
return response.json() as ReturnType<MutationKey>;
});
});
}

export default useMutation;
21 changes: 21 additions & 0 deletions src/useQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useQuery as useRQQuery } from "react-query";

type AnyFunction = (...args: any) => any;

function useQuery<QueryKey extends AnyFunction>(queryKey: QueryKey) {
// This is not a arbitrary convertion. queryKey works differently between the TS world
// and the runtime world:
// - In the TS world, this is a function
// - In the runtime world, this is a string
const queryKeyAsString = queryKey as unknown as string;

return useRQQuery(queryKeyAsString, async () => {
const url = new URL(`${location.origin}/abledev/call-query`);
url.search = new URLSearchParams({ key: queryKeyAsString }).toString();
return fetch(url.toString()).then((response) => {
return response.json() as ReturnType<QueryKey>;
});
});
}

export default useQuery;
23 changes: 23 additions & 0 deletions src/wrapRootComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from "react";
import { QueryClient, QueryClientProvider } from "react-query";

const queryClient = new QueryClient();

function wrapRootComponent(Component: React.FC) {
const AbledevWrapper = (props: any) => {
return (
<QueryClientProvider client={queryClient}>
<Component {...props} />
</QueryClientProvider>
);
};

Object.defineProperty(AbledevWrapper, "name", {
value: `AbledevWrapper(${Component.displayName ?? Component.name})`,
configurable: true,
});

return AbledevWrapper;
}

export default wrapRootComponent;
26 changes: 26 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"include": ["src", "types"],
"compilerOptions": {
"module": "esnext",
"lib": ["dom", "esnext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"moduleResolution": "node",
"jsx": "react",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"ts-node": {
"compilerOptions": { "module": "CommonJS" },
"moduleTypes": {
"**/*.*": "cjs"
}
}
}
58 changes: 58 additions & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import path from "path";
import { Configuration } from "webpack";

const distPath = path.resolve(__dirname, "dist");

const config: Configuration = {
mode: "production",
entry: "./src/index.ts",
output: {
path: distPath,
filename: "abledev-react.js",
library: {
name: "abledev-react",
type: "umd",
},
globalObject: "this",
},
target: "web",
externals: {
react: {
root: "React",
commonjs: "react",
commonjs2: "react",
},
"react-dom": {
root: "ReactDOM",
commonjs: "react-dom",
commonjs2: "react-dom",
},
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader",
options: {
compilerOptions: {
declaration: true,
outDir: distPath,
},
},
},
],
},
],
},
resolve: {
extensions: [".ts", ".tsx", ".ts", ".js"],
},
optimization: {
minimize: false,
},
};

export default config;
Loading

0 comments on commit 582c47c

Please sign in to comment.