diff --git a/app/sdk/jest.config.js b/app/sdk/jest.config.js new file mode 100644 index 00000000..12057e23 --- /dev/null +++ b/app/sdk/jest.config.js @@ -0,0 +1,10 @@ +module.exports = { + roots: ['/src'], + testMatch: [ + "**/__tests__/**/*.+(ts|tsx|js)", + "**/?(*.)+(spec|test).+(ts|tsx|js)" + ], + transform: { + "^.+\\.(ts|tsx)$": "ts-jest" + }, +} diff --git a/app/sdk/package.json b/app/sdk/package.json index 867336ab..0936dc03 100644 --- a/app/sdk/package.json +++ b/app/sdk/package.json @@ -1,18 +1,47 @@ { - "name": "databag", + "name": "databag-client-sdk", "version": "0.0.1", - "description": "Client SDK for building applications on the Databag network.", - "main": "index.js", - "scripts": { - "test": "test" - }, - "repository": { - "type": "git", - "url": "https://github.com/balzack/databag.git/app/sdk" - }, + "description": "Client SDK for Databag Applications", + "author": "Roland Osborne ", + "repository": "https://github.com/balzac/databag/app/sdk", + "license": "Apache-2.0", "keywords": [ - "decentralized" + "typescript", + "databag" ], - "author": "Pierre Balzack", - "license": "Apache-2.0" + "main": "./dist/tsc/main.js", + "types": "./dist/tsc/main.d.ts", + "browser": "./dist/esbuild/browser.js", + "bin": { + "my-cli-tool": "./dist/esbuild/cli.js" + }, + "scripts": { + "cli": "ts-node src/cli.ts", + "lint": "eslint src/ --ext .js,.jsx,.ts,.tsx", + "test": "jest", + "clean": "rm -rf dist build package", + "ts-node": "ts-node", + "docs": "typedoc --entryPoints src/main.ts", + "build": "tsc -p tsconfig.json", + "build-all": "yarn clean && yarn build && yarn esbuild-node && yarn esbuild-browser", + "esbuild-browser": "esbuild src/browser.ts --bundle --minify --sourcemap=external --outfile=dist/esbuild/browser.js", + "esbuild-browser:dev": "esbuild src/browser.ts --bundle --outfile=dist/esbuild/browser.js", + "esbuild-browser:watch": "esbuild src/browser.ts --bundle --watch --outfile=dist/esbuild/browser.js", + "esbuild-node": "esbuild src/cli.ts --bundle --platform=node --minify --sourcemap=external --outfile=dist/esbuild/cli.js", + "esbuild-node:dev": "esbuild src/cli.ts --bundle --platform=node --sourcemap=external --outfile=dist/esbuild/cli.js", + "esbuild-node:watch": "esbuild src/cli.ts --bundle --platform=node --watch --sourcemap=external --outfile=dist/esbuild/cli.js" + }, + "devDependencies": { + "@types/jest": "^27.4.1", + "@types/node": "^17.0.26", + "@typescript-eslint/eslint-plugin": "^5.20.0", + "@typescript-eslint/parser": "^5.20.0", + "esbuild": "^0.14.38", + "eslint": "^8.14.0", + "jest": "27.0.0", + "ts-jest": "^27.1.4", + "ts-node": "^10.7.0", + "typedoc": "^0.22.15", + "typescript": "^4.6.3" + } } diff --git a/app/sdk/src/browser.ts b/app/sdk/src/browser.ts new file mode 100644 index 00000000..0e7cef11 --- /dev/null +++ b/app/sdk/src/browser.ts @@ -0,0 +1,10 @@ +/** + * This file is the entrypoint of browser builds. + * The code executes when loaded in a browser. + */ +import { foo } from './main' + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +(window as any).foo = foo // instead of casting window to any, you can extend the Window interface: https://stackoverflow.com/a/43513740/5433572 + +console.log('Method "foo" was added to the window object. You can try it yourself by just entering "await foo()"') diff --git a/app/sdk/src/cli.ts b/app/sdk/src/cli.ts new file mode 100644 index 00000000..543f46d7 --- /dev/null +++ b/app/sdk/src/cli.ts @@ -0,0 +1,4 @@ +#!/usr/bin/env node +import { foo } from './main' + +foo() diff --git a/app/sdk/src/main.test.ts b/app/sdk/src/main.test.ts new file mode 100644 index 00000000..a93eae26 --- /dev/null +++ b/app/sdk/src/main.test.ts @@ -0,0 +1,9 @@ +import { greet } from './main' + +test('the data is peanut butter', () => { + expect(1).toBe(1) +}); + +test('greeting', () => { + expect(greet('Foo')).toBe('Hello Foo') +}); diff --git a/app/sdk/src/main.ts b/app/sdk/src/main.ts new file mode 100644 index 00000000..5dff870e --- /dev/null +++ b/app/sdk/src/main.ts @@ -0,0 +1,10 @@ +export const delayMillis = (delayMs: number): Promise => new Promise(resolve => setTimeout(resolve, delayMs)); + +export const greet = (name: string): string => `Hello ${name}` + +export const foo = async (): Promise => { + console.log(greet('World')) + await delayMillis(1000) + console.log('done') + return true +} diff --git a/app/sdk/tsconfig.json b/app/sdk/tsconfig.json new file mode 100644 index 00000000..9ebf6dcf --- /dev/null +++ b/app/sdk/tsconfig.json @@ -0,0 +1,38 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "moduleResolution": "node", + "declaration": true, + "strict": true, + "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */, + "strictNullChecks": true /* Enable strict null checks. */, + "strictFunctionTypes": true /* Enable strict checking of function types. */, + "noUnusedLocals": true /* Report errors on unused locals. */, + "noUnusedParameters": true /* Report errors on unused parameters. */, + "noImplicitReturns": true /* Report error when not all code paths in function return a value. */, + "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, + "importHelpers": true, + "skipLibCheck": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "experimentalDecorators": true, + "sourceMap": true, + "outDir": "./dist/tsc/", + "types": [ + "node", + "jest" + ], + "lib": [ + "ES6", + "DOM" + ] + }, + "include": [ + "src/**/*.ts" + ], + "exclude": [ + "node_modules", + "**/*.test.ts" + ] +}