Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Sep 18, 2023
0 parents commit 367bbc9
Show file tree
Hide file tree
Showing 18 changed files with 3,399 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
34 changes: 34 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "es-x"],
"ignorePatterns": ["lib/*", "node_modules/**"],
"extends": [
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:es-x/no-new-in-es2020"
],
"rules": {
"prettier/prettier": "warn",
"no-console": "warn",
"sort-imports": [
"warn",
{
"ignoreCase": true,
"ignoreDeclarationSort": true
}
],
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/no-this-alias": "off",
"no-inner-declarations": "off",
"es-x/no-optional-chaining": "ERROR",
"es-x/no-class-fields": "OFF",
"es-x/no-export-ns-from": "OFF"
}
}
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Tests
on: [push, pull_request]
jobs:
test-node:
strategy:
fail-fast: false
matrix:
node-version: [14, 16, 18]
name: Node.js v${{ matrix.node-version }}
runs-on: ubuntu-latest
steps:
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: "${{ matrix.node-version }}"
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: make node_modules
- name: Run checks
run: make check
- name: Run tests
run: make ci-test
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules/
lib/
coverage/
.nyc_output/
test/browser.html
site/
docs_build/
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
arrowParens: "always"
bracketSpacing: false
endOfLine: "lf"
printWidth: 100
semi: false
singleQuote: true
tabWidth: 4
trailingComma: "es5"
75 changes: 75 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
SRC_FILES := $(shell find src -name '*.ts')
TEST_FILES := $(wildcard test/*.ts)
BIN := ./node_modules/.bin
MOCHA_OPTS := -u tdd -r ts-node/register -r tsconfig-paths/register --extension ts

lib: ${SRC_FILES} package.json tsconfig.json node_modules rollup.config.mjs
@${BIN}/rollup -c && touch lib

.PHONY: test
test: lib node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/mocha ${MOCHA_OPTS} test/*.ts --grep '$(grep)'

.PHONY: test-coverage
test-coverage: lib node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/nyc --reporter=html \
${BIN}/mocha ${MOCHA_OPTS} -R nyan test/*.ts

.PHONY: coverage
coverage: test-coverage
@open coverage/index.html

.PHONY: ci-test
ci-test: lib node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/nyc --reporter=text \
${BIN}/mocha ${MOCHA_OPTS} -R list test/*.ts

.PHONY: check
check: node_modules
@${BIN}/eslint src --ext .ts --max-warnings 0 --format unix && echo "Ok"

.PHONY: format
format: node_modules
@${BIN}/eslint src --ext .ts --fix

test/browser.html: lib $(TEST_FILES) test/rollup.config.mjs node_modules
@${BIN}/rollup -c test/rollup.config.mjs

.PHONY: browser-test
browser-test: test/browser.html
@open test/browser.html

node_modules:
yarn install --non-interactive --frozen-lockfile --ignore-scripts

.PHONY: publish
publish: | distclean node_modules
@git diff-index --quiet HEAD || (echo "Uncommitted changes, please commit first" && exit 1)
@git fetch origin && git diff origin/master --quiet || (echo "Changes not pushed to origin, please push first" && exit 1)
@yarn config set version-tag-prefix "" && yarn config set version-git-message "Version %s"
@yarn publish && git push && git push --tags

docs_build: $(SRC_FILES) node_modules
@${BIN}/typedoc --out docs_build \
--excludeInternal --excludePrivate --excludeProtected \
--includeVersion --readme none \
src/index.ts

.PHONY: deploy-site
deploy-site: | clean docs_build test/browser.html test-coverage
@mkdir -p site
@cp -r docs_build/* site/
@cp -r test/browser.html site/tests.html
@cp -r coverage/ site/coverage/
@${BIN}/gh-pages -d site

.PHONY: clean
clean:
rm -rf lib/ coverage/ docs_build/ site/ test/browser.html

.PHONY: distclean
distclean: clean
rm -rf node_modules/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# @wharfkit/api-client-template

Template for creating new api-client instances for use in Wharf.

## Running Tests

```
make test
```

The browser test suite for the current version of the library is available at: https://wharfkit.github.io/antelope/tests.html
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@wharfkit/api-client-template",
"description": "Template to start building new API clients using Wharf",
"version": "0.0.0",
"homepage": "https://github.com/wharfkit/api-client-template",
"main": "lib/api-client-template.js",
"module": "lib/api-client-template.m.js",
"types": "lib/api-client-template.d.ts",
"browser": {
"buffer": false,
"crypto": false
},
"sideEffects": false,
"files": [
"lib/*",
"src/*"
],
"scripts": {
"prepare": "make"
},
"dependencies": {
"@wharfkit/antelope": "^0.9.1",
"tslib": "^2.0.3"
},
"resolutions": {
"@wharfkit/antelope": "^0.9.1"
},
"devDependencies": {
"@rollup/plugin-alias": "^4.0.3",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.0.0",
"@rollup/plugin-virtual": "^3.0.1",
"@types/bn.js": "^5.1.0",
"@types/elliptic": "^6.4.12",
"@types/mocha": "^10.0.0",
"@types/node": "^18.6.5",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"@wharfkit/mock-data": "^1.0.2",
"chai": "^4.3.4",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-es-x": "^6.0.0",
"eslint-plugin-prettier": "^4.0.0",
"gh-pages": "^5.0.0",
"mocha": "^10.1.0",
"node-fetch": "^2.6.1",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"rollup": "^3.17.2",
"rollup-plugin-dts": "^5.2.0",
"ts-node": "^10.0.0",
"tsconfig-paths": "^4.1.0",
"typedoc": "^0.23.10",
"typescript": "^4.1.2"
}
}
60 changes: 60 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import fs from 'fs'
import path from 'path'
import {URL} from 'url'

import dts from 'rollup-plugin-dts'
import typescript from '@rollup/plugin-typescript'

// eslint-disable-next-line es-x/no-import-meta
const __dirname = path.dirname(new URL(import.meta.url).pathname)
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')))

const external = Object.keys(pkg.dependencies)

export default [
{
input: 'src/index.ts',
output: {
file: pkg.main,
format: 'cjs',
sourcemap: true,
},
plugins: [typescript({target: 'es6'})],
external,
onwarn,
},
{
input: 'src/index.ts',
output: {
file: pkg.module,
format: 'esm',
sourcemap: true,
},
plugins: [typescript({target: 'es2020'})],
external,
onwarn,
},
{
input: 'src/index.ts',
output: {file: pkg.types, format: 'esm'},
onwarn,
plugins: [dts()],
},
]

function onwarn(warning, rollupWarn) {
if (warning.code === 'CIRCULAR_DEPENDENCY') {
// unnecessary warning
return
}
if (
warning.code === 'UNUSED_EXTERNAL_IMPORT' &&
warning.source === 'tslib' &&
warning.names[0] === '__read'
) {
// when using ts with importHelpers: true rollup complains about this
// seems safe to ignore since __read is not actually imported or used anywhere in the resulting bundles
return
}
rollupWarn(warning)
}
24 changes: 24 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {API, APIClient} from '@wharfkit/antelope'

export class ExampleAPI {
constructor(private client: APIClient) {}

/**
* Define the calls for the API
*/
// async get_raw_abi(accountName: NameType) {
// return this.call({
// path: '/v1/chain/get_raw_abi',
// params: {account_name: Name.from(accountName)},
// responseType: GetRawAbiResponse,
// })
// }

// Example for testing
async get_info() {
return this.client.call({
path: '/v1/chain/get_info',
responseType: API.v1.GetInfoResponse,
})
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Export both endpoints and types
export * from './endpoints'
export * as Types from './types'
27 changes: 27 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Define the API response types
*
* Example and mock below
*/

import {Struct} from '@wharfkit/antelope'

@Struct.type('example_response')
export class ExampleResponse extends Struct {
@Struct.field('string') declare foo: string
}

// import {
// Blob,
// Checksum256,
// Name,
// Struct,
// } from '@wharfkit/antelope'

// @Struct.type('get_raw_abi_response')
// export class GetRawAbiResponse extends Struct {
// @Struct.field('name') declare account_name: Name
// @Struct.field('checksum256') declare code_hash: Checksum256
// @Struct.field('checksum256') declare abi_hash: Checksum256
// @Struct.field(Blob) declare abi: Blob
// }
24 changes: 24 additions & 0 deletions test/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {assert} from 'chai'

import {APIClient, FetchProvider} from '@wharfkit/antelope'
import {mockFetch} from '@wharfkit/mock-data'

import {ExampleAPI} from '$lib'

// Setup an APIClient
const client = new APIClient({
provider: new FetchProvider('https://jungle4.greymass.com', {fetch: mockFetch}),
})

// Setup the API
const example = new ExampleAPI(client)

suite('api', function () {
this.slow(200)
this.timeout(10 * 10000)

test('call test api', async function () {
const res = await example.get_info()
assert.equal(res.server_version, '905c5cc9')
})
})
Loading

0 comments on commit 367bbc9

Please sign in to comment.