mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-06-22 16:51:43 +00:00
Update JS SDK version and migrate to the new JS API (#11)
This commit is contained in:
@ -1,36 +1,101 @@
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: 0.3.0-SNAPSHOT
|
||||
*
|
||||
*/
|
||||
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
|
||||
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
|
||||
|
||||
import { FluencePeer } from '@fluencelabs/fluence';
|
||||
import {
|
||||
ResultCodes,
|
||||
RequestFlow,
|
||||
RequestFlowBuilder,
|
||||
CallParams,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||
|
||||
// Services
|
||||
|
||||
//StringExtra
|
||||
//defaultId = "service-id"
|
||||
export interface StringExtraDef {
|
||||
addNameToHello: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||
}
|
||||
|
||||
//addNameToHello: (arg0: string) => string
|
||||
//END StringExtra
|
||||
export function registerStringExtra(service: StringExtraDef): void;
|
||||
export function registerStringExtra(serviceId: string, service: StringExtraDef): void;
|
||||
export function registerStringExtra(peer: FluencePeer, service: StringExtraDef): void;
|
||||
export function registerStringExtra(peer: FluencePeer, serviceId: string, service: StringExtraDef): void;
|
||||
export function registerStringExtra(...args) {
|
||||
let peer: FluencePeer;
|
||||
let serviceId;
|
||||
let service;
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
} else {
|
||||
peer = FluencePeer.default;
|
||||
}
|
||||
|
||||
if (typeof args[0] === 'string') {
|
||||
serviceId = args[0];
|
||||
} else if (typeof args[1] === 'string') {
|
||||
serviceId = args[1];
|
||||
} else {
|
||||
serviceId = 'service-id';
|
||||
}
|
||||
|
||||
if (!(args[0] instanceof FluencePeer) && typeof args[0] === 'object') {
|
||||
service = args[0];
|
||||
} else if (typeof args[1] === 'object') {
|
||||
service = args[1];
|
||||
} else {
|
||||
service = args[2];
|
||||
}
|
||||
|
||||
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||
if (req.serviceId !== serviceId) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.fnName === 'addNameToHello') {
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
arg0: req.tetraplets[0],
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
resp.result = service.addNameToHello(req.args[0], callParams);
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
export async function helloWorld(client: FluenceClient, name: string, config?: {ttl?: number}): Promise<string> {
|
||||
export function helloWorld(name: string, config?: { ttl?: number }): Promise<string>;
|
||||
export function helloWorld(peer: FluencePeer, name: string, config?: { ttl?: number }): Promise<string>;
|
||||
export function helloWorld(...args) {
|
||||
let peer: FluencePeer;
|
||||
let name;
|
||||
let config;
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
name = args[1];
|
||||
config = args[2];
|
||||
} else {
|
||||
peer = FluencePeer.default;
|
||||
name = args[0];
|
||||
config = args[1];
|
||||
}
|
||||
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<string>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
@ -47,20 +112,21 @@ export async function helloWorld(client: FluenceClient, name: string, config?: {
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
|
||||
`,
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
return peer.connectionInfo.connectedRelay;
|
||||
});
|
||||
h.on('getDataSrv', 'name', () => {
|
||||
return name;
|
||||
});
|
||||
h.on('getDataSrv', 'name', () => {return name;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
@ -68,13 +134,12 @@ export async function helloWorld(client: FluenceClient, name: string, config?: {
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for helloWorld');
|
||||
})
|
||||
if(config && config.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
});
|
||||
if (config && config.ttl) {
|
||||
r.withTTL(config.ttl);
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
peer.internals.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user