From 60886757667fa92caab49a61ed4259e1d66b1212 Mon Sep 17 00:00:00 2001 From: Ben Chambule Date: Sun, 25 Aug 2024 13:54:53 +0200 Subject: [PATCH] deleted experimental features --- experimental.js | 865 ----------------------- experimental/examples/bot.js | 102 --- experimental/examples/form.js | 100 --- experimental/examples/form_inputs.js | 75 -- experimental/examples/form_options.js | 107 --- experimental/examples/interceptors.js | 93 --- experimental/examples/json/bot-object.js | 83 --- experimental/examples/json/bot.js | 56 -- experimental/examples/json/bot.json | 206 ------ experimental/examples/json/enquiry.js | 52 -- experimental/examples/json/enquiry.json | 91 --- experimental/examples/redirect.js | 145 ---- experimental/experimental.js | 649 ----------------- experimental/test/form.js | 96 --- experimental/test/form_inputs.js | 70 -- experimental/test/form_options.js | 128 ---- experimental/test/interceptors.js | 138 ---- 17 files changed, 3056 deletions(-) delete mode 100644 experimental.js delete mode 100644 experimental/examples/bot.js delete mode 100644 experimental/examples/form.js delete mode 100644 experimental/examples/form_inputs.js delete mode 100644 experimental/examples/form_options.js delete mode 100644 experimental/examples/interceptors.js delete mode 100644 experimental/examples/json/bot-object.js delete mode 100644 experimental/examples/json/bot.js delete mode 100644 experimental/examples/json/bot.json delete mode 100644 experimental/examples/json/enquiry.js delete mode 100644 experimental/examples/json/enquiry.json delete mode 100644 experimental/examples/redirect.js delete mode 100644 experimental/experimental.js delete mode 100644 experimental/test/form.js delete mode 100644 experimental/test/form_inputs.js delete mode 100644 experimental/test/form_options.js delete mode 100644 experimental/test/interceptors.js diff --git a/experimental.js b/experimental.js deleted file mode 100644 index a6c3ffe..0000000 --- a/experimental.js +++ /dev/null @@ -1,865 +0,0 @@ -const GET = "GET"; -const POST = "POST"; - -/** - * Represents a bot - * @constructor - * @param {string} entrypoint - the first location that will be displayed - * @param {string} description - the description of the bot. - * - */ -class Lottus { - __gets = {}; - __posts = {}; - - debug = false; - entrypoint = "main"; - description = null; - - constructor(){ - } - - /** - * Creates a processor for the method *GET* that returns *message* at location *location*. - * If the message contains a form the default post processor will be attached to the location, - * to process the request from the customer. - * @example - * lottus.at('main', Message { - name: 'name', - body: 'Please tell me your name', - form: Form { inputs: [Array] } - }) - * @param {string} location - the location at which the *message* will be returned - * @param {Message} message - the message to be returned at location *location* - */ - at(location, message){ - if(typeof location !== "string" || !location){ - throw new Error("location must be a non-empty string"); - } - - if(!(message instanceof Message)){ - throw new Error("Argument message must be of type Message"); - } - - this.get(location, message); - - if(!message.final){ - this.post(location, post_processor(this)); - } - } - - /** - * - * @param {string} location - * @param {function|Message} result - * @returns {Lottus} - */ - get(location, result){ - if(typeof location !== "string" || !location){ - throw new Error("location must be a non-empty string"); - } - - if(typeof result === "function" || result instanceof Message){ - this.__gets[location] = result; - return this; - } - - throw new Error("func must be a function or Message"); - } - - /** - * - * @param {string} location - * @param {function|Message} result - * @returns - */ - post(location, result){ - if(typeof location !== "string" || !location){ - throw new Error("location must be a non-empty string"); - } - - if(typeof result === "function" || result instanceof Message){ - this.__posts[location] = result; - return this; - } - - throw new Error("result must be a function or Message"); - } - - /** - * - * @param {string} method - GET | POST - * @param {string} location - * @returns {Message|function} - */ - getProcessor(method, location){ - if(method === GET){ - return this.__gets[location]; - } - - if(method === POST){ - return this.__posts[location]; - } - } - - /** - * - * @param {string} location - * @param {Request} request - * @param {string} method - * @returns - */ - async redirect(location, request, method = GET){ - if(this.debug){ - console.log("Lottus.redirect input", method, location, request); - } - - if(typeof location !== "string"){ - throw new Error("Argument location must be a string"); - } - - if(![GET, POST].includes(method)){ - throw new Error("Argument method must be a string [GET|POST]"); - } - - if(!request){ - request = new Request(); - } - - if(!(request instanceof Request)){ - throw new Error("Argument request must be of type Request"); - } - - let response = new Message(location); - response.body = ""; - response.form = new Form(); - - const processor = this.getProcessor(method, location); - - if(!processor){ - throw new Error("Could not find a processor for "+ location); - } - - let result = null; - if(processor instanceof Message){ - result = processor; - }else if(typeof processor === "function"){ - result = await processor(request, response); - }else{ - throw new Error(method + " " + location +" must be a function or Message"); - } - - for(const param of request.parameters){ - result.addParameter(param); - } - - if(this.debug){ - console.log("Lottus.redirect output", result); - } - - if(!result || !(result instanceof Message)){ - throw new Error("Processor must return a Response response"); - } - - return result; - } - - /** - * - * @param {Request} request - * @param {Session | undefined | null} session - * @returns {Session} - */ - async process(request, session){ - if(this.debug){ - console.log("Lottus.process input", request, session); - } - - if(!(request instanceof Request)){ - throw new Error("Argument request must be of type Request"); - } - - if(session && !(session instanceof Session)){ - throw new Error("Argument session must be of type Session"); - } - - let location = this.entrypoint; - let message = new Message(location); - - message.body = ""; - message.form = new Form(); - - if(session instanceof Session){ - message = session.message; - if(message instanceof Message){ - if(message.form){ - request.form = message.form - } - } - - location = message.name; - } else { - session = new Session(message, request.parameters); - } - - const result = await this.process_request(request, message); - - session.message = new Message(result.name, result.body, result.form); - - if(result.parameters){ - for(const parameter of result.parameters){ - session.addParameter(parameter); - } - } - - if(this.debug){ - console.log("Lottus.process output", session); - } - - return session; - } - - /** - * - * @param {Request} request - * @param {Message} message - * @returns {Message} - */ - async process_request(request, message){ - if(this.debug){ - console.log("process_request input", request, message); - } - - if(!(request instanceof Request)){ - throw new Error("Argument request must be of type Request"); - } - - if(!(message instanceof Message)){ - throw new Error("Argument message must be of type Message") - } - - const processor = this.getProcessor(request.method, message.name); - - if(!processor){ - throw new Error("Could not find a processor for", message.name); - } - - let result = null; - if(processor instanceof Message){ - result = processor; - }else if(typeof processor === "function"){ - result = await processor(request, message); - }else{ - throw new Error(method + " " + location +" must be a function or Message"); - } - - if(this.debug){ - console.log("process_request output", result); - } - - if(!result || !(result instanceof Message)){ - throw new Error("Processor for " + request.method + " " + message.name + " must return a Response object"); - } - - return result; - } -} - - -class TemplateText { - /** - * - * @param {string} text - */ - constructor(text){ - this.text = text; - } - - /** - * - * @param {string} text - */ - append(text){ - if(!this.text){ - this.text = ""; - } - - this.text += text; - } - - /** - * - * @param {Array.} parameters - * @returns {string} - */ - getText(parameters = []){ - result = this.text; - if(result && parameters){ - for(const param of parameters){ - if(param instanceof Parameter){ - result = result.replace("{{"+ param.name +"}}", param.value); - } - } - } - - return result; - } -} - - -class Message { - // body = ""; - // form = new Form(); - /** - * - * @param {string} name - * @param {string} body - * @param {Form} form - */ - constructor(name, body = "", form){ - this.name = name; - this.body = body; - - if(form){ - if(!(form instanceof Form)){ - throw new Error("Argument form must be of type Form"); - } - - if(!form.isEmpty()){ - this.form = form; - } - } - } - - /** - * - * @returns {boolean} - */ - final(){ - if(this.form instanceof Form){ - return this.form.isEmpty(); - } - - return true; - } - - /** - * - * @param {Parameter} parameter - * @returns {void} - */ - addParameter(parameter){ - if(parameter instanceof Parameter){ - if(!this.parameters){ - this.parameters = []; - } - - for(let i = 0; i < this.parameters.length; i++){ - if(this.parameters[i].name === parameter.name){ - this.parameters[i].name = parameter.value; - - return; - } - } - - this.parameters.push(parameter); - } - } -} - -class Form { - /** - * - * @param {Array.} inputs - */ - constructor(inputs = null){ - this.inputs = []; - - if(inputs){ - for(const input of inputs){ - if(!(input instanceof TextInput) && !(input instanceof OptionList)){ - throw new Error("Argument inputs must be a list of TextInput | OptionList") - } - - this.inputs.push(input); - } - } - } - - /** - * - * @param {OptionList | TextInput} input - */ - addInput(input){ - this.inputs.push(input); - } - - /** - * - * @returns boolean - */ - isEmpty(){ - return this.inputs === undefined || this.inputs === null || this.inputs.length === 0; - } -} - - -class OptionList { - /** - * - * @param {Array.