Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
Co-authored-by: Tor Tryggestad Berre <[email protected]>
  • Loading branch information
wkillerud and tor0405 committed May 15, 2024
1 parent f3abfef commit 8f999a2
Show file tree
Hide file tree
Showing 15 changed files with 688 additions and 128 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Run all tests on PR

permissions:
id-token: write
contents: read

on:
pull_request:
branches:
- main
- next

jobs:
run-all-tests:
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Lint
run: npm run lint

- name: Run all tests
run: npm test

- name: Generate and check types
run: npm run types
50 changes: 50 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Release on main

permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance

on:
push:
branches:
- main
- next

# Cancel previous workflows which might still be running
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
release:
name: Release
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Lint
run: npm run lint

- name: Run all tests
run: npm test

- name: Generate and check types
run: npm run types

- name: Release and publish
run: npx semantic-release
env:
ARTIFACTORY_NPM_SECRET: ${{ env.ARTIFACTORY_NPM_SECRET }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ env.ARTIFACTORY_NPM_SECRET }}
144 changes: 16 additions & 128 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,130 +1,18 @@
# Logs
logs
coverage/
.nyc_output/
.idea/
*.iml
node_modules/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
.vscode
package-lock.json
node_modules
node_modules/**/*
.DS_Store
tmp/**/*
.idea
.idea/**/*
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
*.d.ts
!types/global.d.ts
.tap
14 changes: 14 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"singleQuote": true,
"trailingComma": "all",
"useTabs": true,
"overrides": [
{
"files": [".prettierrc", "*.json", "*.yml", ".travis.yml"],
"options": {
"useTabs": false,
"tabWidth": 2
}
}
]
}
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# `@podium/bridge`

This package is a bridge designed to pass [JSON-RPC 2.0](https://www.jsonrpc.org/specification) messages between a web application and a native web view.

## Usage

To install:

```sh
npm install @podium/bridge
```

Import the bridge in your client-side bundle:

```js
// The bridge is made available on `window.podium.bridge`
import '@podium/bridge';

/** @type {import("@podium/bridge").PodiumBridge} */
const bridge = window.podium.bridge;

// You can listen for incoming messages, which can either be RpcRequest or RpcResponse
bridge.on('global/authentication', (message) => {
const request =
/** @type {import("@podium/bridge").RpcRequest<{ token?: string }>} */ (
message
);

if (typeof request.token === 'string') {
// logged in
} else {
// logged out
}
});

// You can trigger notifications (one-way messages)
bridge.notification({
method: 'global/authentication',
params: { token: null },
});

// And you can call methods and await the response
/** @type {import("@podium/bridge").RpcResponse<{ c: string }>} */
const response = await bridge.call({
method: 'document/native-feature',
params: { a: 'foo', b: 'bar' },
});
```

## API

### `bridge.on`

Add a listener for incoming messages for a given method name.

```js
import '@podium/bridge';

/** @type {import("@podium/bridge").PodiumBridge} */
const bridge = window.podium.bridge;

bridge.on('global/authentication', (message) => {
const request =
/** @type {import("@podium/bridge").RpcRequest<{ token?: string }>} */ (
message
);

if (typeof request.token === 'string') {
// logged in
} else {
// logged out
}
});
```

### `bridge.notification`

Send a [notification](https://www.jsonrpc.org/specification#notification) (one-way message).

```js
import '@podium/bridge';

/** @type {import("@podium/bridge").PodiumBridge} */
const bridge = window.podium.bridge;

bridge.notification({
method: 'global/authentication',
params: { token: null },
});
```

### `bridge.call`

Send a [request](https://www.jsonrpc.org/specification#request_object) and await a [response](https://www.jsonrpc.org/specification#response_object).

```js
import '@podium/bridge';

/** @type {import("@podium/bridge").PodiumBridge} */
const bridge = window.podium.bridge;

/** @type {import("@podium/bridge").RpcResponse<{ c: string }>} */
const response = await bridge.call({
method: 'document/native-feature',
params: { a: 'foo', b: 'bar' },
});
```
19 changes: 19 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import js from '@eslint/js';
import prettierConfig from 'eslint-config-prettier';
import prettierPlugin from 'eslint-plugin-prettier';
import globals from 'globals';

export default [
js.configs.recommended,
prettierConfig,
{
plugins: {
prettier: prettierPlugin,
},
languageOptions: {
globals: {
...globals.browser,
},
},
},
];
2 changes: 2 additions & 0 deletions fixup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
cat >> types/bridge.d.ts < types/global.d.ts
Loading

0 comments on commit 8f999a2

Please sign in to comment.