@superfaceai/one-sdk-cloudflare
Advanced tools
Comparing version 1.0.0-beta.0 to 1.0.0-beta.1
@@ -1,2 +0,2 @@ | ||
import { AppContext, FileSystem, Network, TextCoder, Timers, WasiContext, Persistence } from './interfaces.js'; | ||
import { AppContext, FileSystem, Network, Persistence, TextCoder, Timers, WasiContext } from './interfaces.js'; | ||
import { SecurityValuesMap } from './security.js'; | ||
@@ -39,2 +39,3 @@ /** Async mutex allows us to synchronize multiple async tasks. | ||
private metricsState; | ||
private readonly userAgent; | ||
constructor(dependencies: { | ||
@@ -47,2 +48,3 @@ network: Network; | ||
}, options: { | ||
userAgent?: string; | ||
metricsTimeout?: number; | ||
@@ -49,0 +51,0 @@ }); |
import { Asyncify } from './asyncify.js'; | ||
import { PerformError, UnexpectedError, UninitializedError, ValidationError, WasiErrno, WasiError } from './error.js'; | ||
import { HandleMap } from './handle_map.js'; | ||
import { PerformError, UnexpectedError, UninitializedError, ValidationError, WasiErrno, WasiError } from './error.js'; | ||
import * as sf_host from './sf_host.js'; | ||
@@ -104,2 +104,3 @@ class ReadableStreamAdapter { | ||
this.requests = new HandleMap(); | ||
this.userAgent = options?.userAgent; | ||
this.metricsState = { | ||
@@ -106,0 +107,0 @@ timeout: options.metricsTimeout ?? 1000, |
@@ -24,2 +24,3 @@ import type { SecurityValuesMap } from './common/index.js'; | ||
sendMetrics(): Promise<void>; | ||
private get userAgent(); | ||
} | ||
@@ -26,0 +27,0 @@ export declare class OneClient { |
import { WASI } from '@cloudflare/workers-wasi'; | ||
import { App, HandleMap, UnexpectedError } from './common/index.js'; | ||
export { PerformError, UnexpectedError } from './common/error.js'; | ||
// @ts-ignore | ||
import coreModule from '../assets/core-async.wasm'; | ||
import { ErrorCode, HostError, WasiErrno, WasiError } from './common/index.js'; | ||
const pkg = require('../package.json'); | ||
export { PerformError, UnexpectedError } from './common/error.js'; | ||
class CfwTextCoder { | ||
@@ -168,4 +169,5 @@ constructor() { | ||
class CfwPersistence { | ||
constructor(token, superfaceApiUrl) { | ||
constructor(token, superfaceApiUrl, userAgent) { | ||
this.token = token; | ||
this.userAgent = userAgent; | ||
if (superfaceApiUrl !== undefined) { | ||
@@ -188,2 +190,5 @@ this.insightsUrl = `${superfaceApiUrl}/insights/sdk_event`; | ||
} | ||
if (this.userAgent !== undefined) { | ||
headers['user-agent'] = this.userAgent; | ||
} | ||
await fetch(`${this.insightsUrl}/batch`, { | ||
@@ -210,3 +215,3 @@ method: 'POST', | ||
network: new CfwNetwork(), | ||
persistence: new CfwPersistence(options.token, options.superfaceApiUrl) | ||
persistence: new CfwPersistence(options.token, options.superfaceApiUrl, this.userAgent) | ||
}, { metricsTimeout: 0 }); | ||
@@ -219,5 +224,9 @@ } | ||
} | ||
console.log('INIT useragent', this.userAgent); | ||
await this.app.loadCoreModule(coreModule); | ||
const wasi = new WASI({ | ||
env: this.options.env | ||
env: { | ||
ONESDK_DEFAULT_USERAGENT: this.userAgent, | ||
...this.options.env | ||
} | ||
}); | ||
@@ -248,2 +257,5 @@ await this.app.init(new CfwWasiCompat(wasi)); | ||
} | ||
get userAgent() { | ||
return `one-sdk-cloudflare/${pkg.version}`; | ||
} | ||
} | ||
@@ -250,0 +262,0 @@ export class OneClient { |
{ | ||
"name": "@superfaceai/one-sdk-cloudflare", | ||
"version": "1.0.0-beta.0", | ||
"version": "1.0.0-beta.1", | ||
"exports": { | ||
@@ -5,0 +5,0 @@ ".": "./dist/index.js", |
@@ -13,3 +13,3 @@ [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) | ||
[![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-cloudflare/v/beta) | ||
[![npm](https://img.shields.io/npm/v/@superfaceai/one-sdk-cloudflare/beta.svg)](https://www.npmjs.com/package/@superfaceai/one-sdk-cloudflare/v/beta) | ||
@@ -78,7 +78,10 @@ `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. | ||
```js | ||
import { OneClient, PerformError, UnexpectedError } from '@superfaceai/one-sdk-cloudflare'; | ||
import { | ||
OneClient, | ||
PerformError, | ||
} from '@superfaceai/one-sdk-cloudflare'; | ||
import profileFile from '../superface/[scope.]<name>.profile'; | ||
import mapFile from '../superface/[scope.]<name>.<providerName>.map.js'; | ||
import providerFile from '../superface/<providerName>.provider.json'; | ||
import profileFile from './superface/[scope.]<name>.profile'; | ||
import mapFile from './superface/[scope.]<name>.<providerName>.map.js'; | ||
import providerFile from './superface/<providerName>.provider.json'; | ||
@@ -104,3 +107,5 @@ export default { | ||
// Input parameters as defined in profile: | ||
'<key>': '<value>' | ||
{ | ||
'<key>': '<value>', | ||
}, | ||
// provider configuration | ||
@@ -139,2 +144,17 @@ { | ||
And Wrangler configuration to properly load all files: | ||
``` | ||
name = "example" | ||
main = "index.ts" | ||
compatibility_date = "2023-04-18" | ||
workers_dev = true | ||
rules = [ | ||
{ type = "Data", globs = ["**/*.map.js", "**/*.profile", "**/*.provider.json"], fallthrough = true } | ||
] | ||
``` | ||
Check full demo with [Shopify](https://github.com/superfaceai/demo-cloudflare-shopify/tree/main) use-cases and more details. | ||
@@ -141,0 +161,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
4686366
1386
175