Opt out defaults (#43)

* Change the wat the withDefaults() words. Now it is "disable default injections" instead of "enable default injections"
This commit is contained in:
Pavel 2021-04-21 19:56:19 +03:00 committed by GitHub
parent f9d7a1c875
commit d84d9fd7ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 29 deletions

View File

@ -123,7 +123,6 @@ export const checkConnection = async (client: FluenceClient, ttl?: number): Prom
const callbackService = '_callback';
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`(seq
(call init_relay ("op" "identity") [msg] result)

View File

@ -2,6 +2,7 @@ import { checkConnection, createClient, FluenceClient } from '../../FluenceClien
import Multiaddr from 'multiaddr';
import { nodes } from '../connection';
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder';
import { error } from 'loglevel';
let client: FluenceClient;
@ -19,7 +20,6 @@ describe('Typescript usage suite', () => {
// act
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`(seq
(call init_relay ("op" "identity") ["hello world!"] result)
@ -77,9 +77,7 @@ describe('Typescript usage suite', () => {
data.set('c', 'some c');
data.set('d', 'some d');
await client1.initiateFlow(
new RequestFlowBuilder().withDefaults().withRawScript(script).withVariables(data).build(),
);
await client1.initiateFlow(new RequestFlowBuilder().withRawScript(script).withVariables(data).build());
let res = await resMakingPromise;
expect(res).toEqual(['some a', 'some b', 'some c', 'some d']);
@ -189,7 +187,6 @@ describe('Typescript usage suite', () => {
it('xor handling should work with connected client', async function () {
// arrange
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
@ -214,7 +211,6 @@ describe('Typescript usage suite', () => {
it('xor handling should work with local client', async function () {
// arrange
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(call %init_peer_id% ("service" "fails") [])
@ -244,9 +240,11 @@ describe('Typescript usage suite', () => {
const res = callIdentifyOnInitPeerId(client);
// assert
await expect(res).rejects.toMatch(
"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''",
);
await expect(res).rejects.toMatchObject({
error:
"Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'",
instruction: 'call %init_peer_id% ("peer" "identify") [] res',
});
});
});

View File

@ -19,7 +19,6 @@ describe('== AIR suite', () => {
// prettier-ignore
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.buildAsFetch<string[]>(serviceId, fnName);
@ -60,7 +59,6 @@ describe('== AIR suite', () => {
const script = `(incorrect)`;
// prettier-ignore
const [request, error] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.buildWithErrorHandling();
@ -77,7 +75,6 @@ describe('== AIR suite', () => {
const script = `(null)`;
// prettier-ignore
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withTTL(1)
.withRawScript(script)
.buildAsFetch();
@ -99,7 +96,6 @@ describe('== AIR suite', () => {
// prettier-ignore
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.withVariable('arg1', 'hello')
.buildAsFetch<string[]>(serviceId, fnName);
@ -142,7 +138,7 @@ describe('== AIR suite', () => {
(call %init_peer_id% ("${makeDataServiceId}" "${makeDataFnName}") [] result)
(call %init_peer_id% ("${getDataServiceId}" "${getDataFnName}") [result.$.field])
)`;
await client.initiateFlow(new RequestFlowBuilder().withDefaults().withRawScript(script).build());
await client.initiateFlow(new RequestFlowBuilder().withRawScript(script).build());
// assert
const tetraplet = res.tetraplets[0][0];
@ -191,7 +187,7 @@ describe('== AIR suite', () => {
(call %init_peer_id% ("${serviceId2}" "${fnName2}") ["${arg2}"] result2))
(call %init_peer_id% ("${serviceId3}" "${fnName3}") [result1 result2]))
`;
await client.initiateFlow(new RequestFlowBuilder().withDefaults().withRawScript(script).build());
await client.initiateFlow(new RequestFlowBuilder().withRawScript(script).build());
// assert
expect(res1).toEqual(arg1);

View File

@ -45,7 +45,6 @@ export const sendParticle = async (
onError?: (err) => void,
): Promise<string> => {
const [req, errorPromise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(particle.script)
.withVariables(particle.data)
.withTTL(particle.ttl)
@ -149,7 +148,6 @@ export const sendParticleAsFetch = async <T>(
callbackServiceId: string = '_callback',
): Promise<T> => {
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(particle.script)
.withVariables(particle.data)
.withTTL(particle.ttl)

View File

@ -95,6 +95,10 @@ const wrapWithInjectRelayScript = (script: string): string => {
* Builder class for configuring and creating Request Flows
*/
export class RequestFlowBuilder {
private shouldInjectVariables: boolean = true;
private shouldInjectErrorHandling: boolean = true;
private shouldInjectRelay: boolean = true;
private ttl: number = DEFAULT_TTL;
private variables = new Map<string, any>();
private handlerConfigs: Array<(handler: AquaCallHandler, request: RequestFlow) => void> = [];
@ -106,6 +110,16 @@ export class RequestFlowBuilder {
* Builds the Request flow with current configuration
*/
build() {
if (this.shouldInjectRelay) {
this.injectRelay();
}
if (this.shouldInjectVariables) {
this.injectVariables();
}
if (this.shouldInjectErrorHandling) {
this.wrapWithXor();
}
const sb = new ScriptBuilder();
for (let action of this.buildScriptActions) {
action(sb);
@ -129,14 +143,13 @@ export class RequestFlowBuilder {
}
/**
* Provides necessary defaults when building requests by hand without the Aquamarine language compiler
* Includes: relay and variable injection, error handling with top-level xor wrap
* Removes necessary defaults when building requests by hand without the Aquamarine language compiler
* Removed features include: relay and variable injection, error handling with top-level xor wrap
*/
withDefaults(): RequestFlowBuilder {
this.injectRelay();
this.injectVariables();
this.wrapWithXor();
disableInjections(): RequestFlowBuilder {
this.shouldInjectRelay = false;
this.shouldInjectVariables = false;
this.shouldInjectErrorHandling = false;
return this;
}

View File

@ -55,7 +55,6 @@ const requestResponse = async <T>(
`;
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.withVariables(data)
.withTTL(ttl)
@ -73,7 +72,6 @@ const requestResponse = async <T>(
export const getModules = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
let callbackFn = 'getModules';
const [req, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
@ -102,7 +100,6 @@ export const getModules = async (client: FluenceClient, ttl?: number): Promise<s
export const getInterfaces = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
let callbackFn = 'getInterfaces';
const [req, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
@ -169,7 +166,6 @@ export const uploadModule = async (
data.set('myPeerId', client.selfPeerId);
const [req, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq