Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
freaz committed Sep 11, 2023
1 parent 1f63476 commit f67a6ca
Show file tree
Hide file tree
Showing 33 changed files with 940 additions and 818 deletions.
6 changes: 0 additions & 6 deletions host/javascript/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions packages/cloudflare_worker_host/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
node_modules/
LICENSE
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -69,73 +69,7 @@ The final structure should look like this:
└── mailchimp.provider.json
```

## Use in Node.js

Create `index.mjs` file with following content and update:

```js
import {
OneClient,
PerformError,
UnexpectedError,
ValidationError,
} from '@superfaceai/one-sdk/node';

const client = new OneClient();
const profile = await client.getProfile('<profileName>');

try {
const result = await profile.getUseCase('<usecaseName>').perform(
{
// Input parameters as defined in profile:
'<key>': '<value>',
},
{
provider: '<providerName>',
parameters: {
// Provider specific integration parameters:
'<integrationParameterName>': '<integrationParameterValue>',
},
security: {
// Provider specific security values:
'<securityValueId>': {
// Security values as described in provider or on profile page
},
},
}
);

console.log('RESULT:', result);
} catch (e) {
if (e instanceof PerformError) {
console.log('ERROR RESULT:', e.errorResult);
} else if (e instanceof ValidationError) {
console.error('VALIDATION ERROR:', e.message);
} else if (e instanceof UnexpectedError) {
console.error('ERROR:', e);
} else {
throw e;
}
}
```

Then run the script with:

```shell
node index.mjs
```

_Note: If you are running Node.js before version `18.17.0` you need to enable WASI by providing flag to Node.js:_

```shell
node --experimental-wasi-unstable-preview1 index.mjs
```

---

OneSDK uses [ECMAScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). More on using ECMAScript modules is well described in [Pure ESM Package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) guide.

## Use in Cloudflare
## Use

The main difference compared to Node.js is a need to use a virtual filesystem to load the Comlink files. It is needed due to the deployment process, where all files need to be bundled together.

Expand Down Expand Up @@ -206,16 +140,11 @@ Check full demo with [Shopify](https://github.com/superfaceai/demo-cloudflare-sh
The next-gen OneSDK is still in beta stage and several features are not yet implemented. We welcome any and all feedback. The current limitations include:

- OneSDK Client can't be instantiated in the global scope

- We discovered Cloudflare is not allowing synchronisation between requests. We need to make sure, that two different requests are not accessing OneSDK Core at the same time. [The problem](https://zuplo.com/blog/the-script-will-never-generate-a-response-on-cloudflare-workers).

- Build-time integrations only

- Currently the maps (integration glue) needs to be bundled with the worker at the build time
- Future OneSDK will be fetching the maps at the runtime to enable dynamic healing and recovery

### Cloudflare Workers specific

- The compiled WASM OneSDK is hitting the 1MB limit of the Cloudflare workers free tier

## License
Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare_worker_host/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0-alpha.0
33 changes: 33 additions & 0 deletions packages/cloudflare_worker_host/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@superfaceai/one-sdk-cloudflare",
"version": "1.0.0-alpha.0",
"exports": {
".": "./dist/index.js",
"./common": "./dist/common/index.js",
"./assets/core-async.wasm": "./assets/core-async.wasm"
},
"type": "module",
"license": "MIT",
"engines": {
"node": ">=18"
},
"dependencies": {
"@cloudflare/workers-wasi": "^0.0.5"
},
"devDependencies": {
"@types/node": "^20.3.3",
"rimraf": "^5.0.1",
"typescript": "^5.1.6"
},
"scripts": {
"check": "tsc -p tsconfig.json --noEmit",
"clean": "rimraf dist",
"prepack": "cp ../../LICENSE .",
"postpack": "rm LICENSE",
"build": "yarn clean && tsc -p tsconfig.json"
},
"files": [
"assets/**/*",
"dist/**/*"
]
}
File renamed without changes.
13 changes: 13 additions & 0 deletions packages/cloudflare_worker_host/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../javascript_common/tsconfig.base.json",
"compilerOptions": {
"rootDirs": [
"./src",
"../javascript_common/src"
],
"outDir": "./dist"
},
"include": [
"./src/**/*.ts"
]
}
8 changes: 8 additions & 0 deletions packages/javascript_common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@superfaceai/one-sdk-common",
"private": true,
"version": "1.0.0",
"main": "index.js",
"repository": "https://github.com/superfaceai/one-sdk",
"license": "MIT"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@
"strict": true,
"strictPropertyInitialization": true,
"target": "ES2020",
"typeRoots": [
"node_modules/@types"
],
"module": "ES2022",
"moduleResolution": "bundler",
"rootDir": "./src",
"outDir": "./",
"declaration": true,
},
"include": [
"src/**/*.ts"
],
"exclude": []
"declaration": true
}
}
10 changes: 10 additions & 0 deletions packages/javascript_common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
},
"include": [
"src/**/*.ts"
]
}
3 changes: 3 additions & 0 deletions packages/nodejs_host/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
node_modules/
LICENSE
8 changes: 8 additions & 0 deletions packages/nodejs_host/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
152 changes: 152 additions & 0 deletions packages/nodejs_host/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
[Website](https://superface.ai) | [Get Started](https://superface.ai/docs/getting-started) | [Documentation](https://superface.ai/docs) | [GitHub Discussions](https://sfc.is/discussions) | [Twitter](https://twitter.com/superfaceai) | [Support](https://superface.ai/support)
<br />
<br />
<img src="https://github.com/superfaceai/one-sdk/raw/main/docs/LogoGreen.png" alt="Superface" width="100" height="100">

# Superface OneSDK

**One SDK for all the APIs you want to integrate with.**

[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/superfaceai/one-sdk/ci_cd.yml)](https://github.com/superfaceai/one-sdk/actions/workflows/ci_cd.yml)
[![license](https://img.shields.io/npm/l/@superfaceai/one-sdk)](LICENSE)
[![GitHub Discussions](https://img.shields.io/github/discussions/superfaceai/.github?logo=github&logoColor=fff)](https://github.com/orgs/superfaceai/discussions)
[![npm](https://img.shields.io/npm/v/@superfaceai/one-sdk/beta.svg)](https://www.npmjs.com/package/@superfaceai/one-sdk/v/beta)

`OneClient` is a universal API client which provides an unparalleled developer experience for every HTTP API. It enhances resiliency to API changes, and comes with built-in integration monitoring and provider failover.

For more details about Superface, visit [How it Works](https://superface.ai/how-it-works) and [Get Started](https://superface.ai/docs/getting-started).

## Important Links

- [Superface website](https://superface.ai)
- [Get Started](https://superface.ai/docs/getting-started)
- [Documentation](https://superface.ai/docs)
- [Support](https://superface.ai/support)

## Install

To install OneSDK into the project, run:

```shell
npm install @superfaceai/one-sdk@beta
```

## Setup

OneClient uses three files (also called Comlink) which together make the integration:

- **Profile** - describe business capabilities apart from the implementation details, what is expected as input and what will be the result. Profile name have optional scope before `/` and required name `[scope/]<name>`
- **Provider** - Define a provider's API services and security schemes to use in a Comlink Map
- **Map** - describe implementation details for fulfilling a business capability from a Comlink Profile

To glue all the parts together, OneClient uses name and file structure convention.

```
.
└── superface/ - directory with all the Comlinks in project root
├── <profileScope>.<profileName>.profile - profile file
├── <providerName>.provider.json - provider file
├── <profileScope>.<profileName>.<providerName>.map.js - map file
└── ... - repeat for all the Comlinks
```

### Send email example

As an example, lets send an email with [Mailchimp](https://github.com/superfaceai/one-sdk/blob/main/examples/maps/src/mailchimp.provider.json). The use-case is described in the profile [communication/send-email](https://github.com/superfaceai/one-sdk/blob/main/examples/maps/src/communication.send-email.profile) and the map with [implementation](https://github.com/superfaceai/one-sdk/blob/feat/superface_assets_convention/examples/maps/src/communication.send-email.mailchimp.map.js).

1. Start with creating a new directory `superface` in the root of your project.
2. Add the profile. Because profile name contains have scope we need to replace `/` with `.`. So, the profile with name `communication/send-email` have corresponding filename `communication.send-email.profile`.
3. Add the provider. The provider name is the same as the filename. So the provider with name `mailchimp` have corresponding filename `mailchimp.provider.json`.
4. Add the map. Map connects profile and provider, so the filename is consists of both as well `communication.send-email.mailchimp.map.js`.

The final structure should look like this:

```
.
└── superface/
├── communication.send-email.mailchimp.map.js
├── communication.send-email.profile
└── mailchimp.provider.json
```

## Use

Create `index.mjs` file with following content and update:

```js
import {
OneClient,
PerformError,
UnexpectedError,
ValidationError,
} from '@superfaceai/one-sdk/node';

const client = new OneClient();
const profile = await client.getProfile('<profileName>');

try {
const result = await profile.getUseCase('<usecaseName>').perform(
{
// Input parameters as defined in profile:
'<key>': '<value>',
},
{
provider: '<providerName>',
parameters: {
// Provider specific integration parameters:
'<integrationParameterName>': '<integrationParameterValue>',
},
security: {
// Provider specific security values:
'<securityValueId>': {
// Security values as described in provider or on profile page
},
},
}
);

console.log('RESULT:', result);
} catch (e) {
if (e instanceof PerformError) {
console.log('ERROR RESULT:', e.errorResult);
} else if (e instanceof ValidationError) {
console.error('VALIDATION ERROR:', e.message);
} else if (e instanceof UnexpectedError) {
console.error('ERROR:', e);
} else {
throw e;
}
}
```

Then run the script with:

```shell
node index.mjs
```

_Note: If you are running Node.js before version `18.17.0` you need to enable WASI by providing flag to Node.js:_

```shell
node --experimental-wasi-unstable-preview1 index.mjs
```

---

OneSDK uses [ECMAScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). More on using ECMAScript modules is well described in [Pure ESM Package](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) guide.

## Todos & limitations

The next-gen OneSDK is still in beta stage and several features are not yet implemented. We welcome any and all feedback. The current limitations include:

- OneSDK Client can't be instantiated in the global scope
- We discovered Cloudflare is not allowing synchronisation between requests. We need to make sure, that two different requests are not accessing OneSDK Core at the same time. [The problem](https://zuplo.com/blog/the-script-will-never-generate-a-response-on-cloudflare-workers).
- Build-time integrations only
- Currently the maps (integration glue) needs to be bundled with the worker at the build time
- Future OneSDK will be fetching the maps at the runtime to enable dynamic healing and recovery

## License

OneSDK is licensed under the [MIT License](LICENSE).

© 2023 Superface s.r.o.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ const jestConfig: JestConfigWithTsJest = {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.m?[tj]sx?$': [
'ts-jest',
{
useESM: true,
},
],
},
}
export default jestConfig
};

export default jestConfig;
Loading

0 comments on commit f67a6ca

Please sign in to comment.