Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SparoHawk committed Mar 10, 2023
0 parents commit 3fc5512
Show file tree
Hide file tree
Showing 16 changed files with 7,278 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI
on: [push]
jobs:
build:
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- name: Install deps and build (with cache)
uses: bahmutov/npm-install@v1

- name: Lint
run: yarn lint

- name: Test
run: yarn test --ci --coverage --maxWorkers=2

- name: Build
run: yarn build
12 changes: 12 additions & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: size
on: [pull_request]
jobs:
size:
runs-on: ubuntu-latest
env:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v1
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
.DS_Store
node_modules
dist
directory-test
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Rogelio Morey

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.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Kanvas Core JS
62 changes: 62 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"version": "0.1.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"scripts": {
"start": "tsdx watch",
"build": "rm -rf dist && tsdx build && tsc --emitDeclarationOnly",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "ts-patch install -s && yarn build",
"size": "size-limit",
"analyze": "size-limit --why",
"generate:test": "rm -rf directory-test && npx create-next-app@latest directory-test --use-yarn --example 'https://github.com/vercel/next-learn/tree/master/basics/learn-starter'"
},
"peerDependencies": {},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"name": "@kanvas/core",
"author": "Rogelio Morey",
"module": "dist/kanvas-core.esm.js",
"size-limit": [
{
"path": "dist/kanvas-core.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/kanvas-core.esm.js",
"limit": "10 KB"
}
],
"devDependencies": {
"@size-limit/preset-small-lib": "^8.2.4",
"husky": "^8.0.3",
"size-limit": "^8.2.4",
"ts-patch": "^2.1.0",
"tsdx": "^0.14.1",
"tslib": "^2.5.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@apollo/client": "^3.7.10",
"graphql": "^16.6.0",
"typescript-transform-paths": "^3.4.6"
}
}
67 changes: 67 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, RequestHandler, NormalizedCacheObject } from "@apollo/client";
import { setContext } from '@apollo/client/link/context';

import { Auth } from './modules'

type Middleware = (ApolloLink | RequestHandler);
export type ClientType = ApolloClient<NormalizedCacheObject>;

interface Options {
url: string;
key: string;
middlewares?: Middleware[];
}

export function genericAuthMiddleware(authKey: string) {
return setContext(async (_, context) => {
const headers = {
...context.headers,
'Authorization': authKey ? `Bearer ${authKey}` : '',
}

return { headers };
})
}

export default class KanvasCore {
public client: ClientType;

public auth: Auth;

constructor(protected options: Options) {
this.client = new ApolloClient({
link: this.generateLink(),
cache: new InMemoryCache(),
});

this.auth = new Auth(this.client);
}

protected generateURL() {
return new HttpLink({ uri: this.options.url });
}

protected generateMiddleware() {
return new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext(({ headers = {} }) => {
return {
headers: {
...headers,
'X-Kanvas-App': this.options.key,
},
};
});

return forward(operation);
});
}

protected generateLink(): ApolloLink {
return ApolloLink.from([
...(this.options.middlewares || []),
this.generateMiddleware(),
this.generateURL(),
])
}
}
15 changes: 15 additions & 0 deletions src/modules/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ClientType } from '../../index';
import { LOGIN_MUTATION } from '../../mutations'
import { AuthenticationInterface } from '../../types'

export class Auth {
constructor(protected client: ClientType) {}

public async login(email: string, password: string): Promise<AuthenticationInterface> {
const data = { email, password };
const response = await this.client.mutate({
mutation: LOGIN_MUTATION, variables: { data }
});
return response.data.login as AuthenticationInterface;
}
}
1 change: 1 addition & 0 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './auth'
15 changes: 15 additions & 0 deletions src/mutations/auth.mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { gql } from "@apollo/client";

export const LOGIN_MUTATION = gql`
mutation login($data: LoginInput!) {
login(data: $data) {
id
token
refresh_token
token_expires
refresh_token_expires
time
timezone
}
}
`;
1 change: 1 addition & 0 deletions src/mutations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './auth.mutation'
9 changes: 9 additions & 0 deletions src/types/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface AuthenticationInterface {
id: number;
token: string;
refresh_token: string;
token_expires: string;
refresh_token_expires: string;
time: string;
timezone: string;
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './auth';
7 changes: 7 additions & 0 deletions test/blah.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sum } from '../src';

describe('blah', () => {
it('works', () => {
expect(sum(1, 1)).toEqual(2);
});
});
49 changes: 49 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
"include": ["src", "types"],
"exclude": ["test", "node_modules"],
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "esnext"],
"baseUrl": "src",
"paths": {
"core/*": ["core/*"],
"modules/*": ["modules/*"],
"types/*": ["types/*"],
},
"importHelpers": true,
// output .d.ts declaration files for consumers
"declaration": true,
// output .js.map sourcemap files for consumers
"sourceMap": true,
// match output dir to input dir. e.g. dist/index instead of dist/src/index
"rootDir": "./src",
// stricter type-checking for stronger correctness. Recommended by TS
"strict": true,
// linter checks for common issues
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
"noUnusedLocals": true,
"noUnusedParameters": true,
// use Node's module resolution algorithm, instead of the legacy TS one
"moduleResolution": "node",
// transpile JSX to React.createElement
"jsx": "react",
// interop between ESM and CJS modules. Recommended by TS
"esModuleInterop": true,
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS
"skipLibCheck": true,
// error out if import and file system have a casing mismatch. Recommended by TS
"forceConsistentCasingInFileNames": true,
// Note: In order to transform *both* js and d.ts files, you need to add both of the below lines to plugins
"plugins": [
// Transform paths in output .js files
{ "transform": "typescript-transform-paths" },

// Transform paths in output .d.ts files (Include this line if you output declarations files)
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
]
}
}
Loading

0 comments on commit 3fc5512

Please sign in to comment.