GeezerVison is showing github.com — refactor: api url parse + override json response · eas-framework/eas-framework@3dcdac0
This repository was archived by the owner on Oct 6, 2024. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings BuildInComponents/Components
tests/core/Website/WWW/server/api
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/BuildInComponents/Components/connect.ts +4-9Lines changed: 4 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,7 +50,7 @@ export function addFinalizeBuild(pageData: StringTracker, sessionInfo: SessionBu | 50 | 50 | message:${typeof i.message == 'string' ? `"${i.message}"` : i.message}, | 51 | 51 | validator:[${(i.validator && i.validator.map(compileValues).join(',')) || ''}] | 52 | 52 | }` | 53 | | - }, {returnData: true}) | | 53 | + }, { returnData: true }) | 54 | 54 | } | 55 | 55 |
| 56 | 56 | export async function handelConnector(thisPage: any, connector: any) { | @@ -59,19 +59,14 @@ export async function handelConnector(thisPage: any, connector: any) { | 59 | 59 |
| 60 | 60 | thisPage.setResponse(''); | 61 | 61 |
| 62 | | - const betterJSON = (obj: any) => { | 63 | | - thisPage.Response.setHeader('Content-Type', 'application/json'); | 64 | | - thisPage.Response.end(JSON.stringify(obj)); | 65 | | - } | 66 | | - | 67 | 62 | if (!connector.validator.length || isValid === true) | 68 | | - betterJSON(await connector.sendTo(...values)); | | 63 | + thisPage.Response.json(await connector.sendTo(...values)); | 69 | 64 |
| 70 | 65 | else if (connector.notValid) | 71 | | - betterJSON(await connector.notValid(...<any>isValid)); | | 66 | + thisPage.Response.json(await connector.notValid(...<any>isValid)); | 72 | 67 |
| 73 | 68 | else if (connector.message) | 74 | | - betterJSON({ | | 69 | + thisPage.Response.json({ | 75 | 70 | error: typeof connector.message == 'string' ? connector.message : (<any>isValid).shift() | 76 | 71 | }); | 77 | 72 | else | |
Collapse file Copy file name to clipboardExpand all lines: src/CompileCode/InsertComponent.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ import { BasicSettings } from '../RunTimeBuild/SearchFileSystem'; | 3 | 3 | import { ParseDebugInfo, CreateFilePath, PathTypes, AddDebugInfo } from './XMLHelpers/CodeInfoAndDebug'; | 4 | 4 | import { AllBuildIn, IsInclude, StartCompiling } from '../BuildInComponents/index'; | 5 | 5 | import StringTracker, { StringTrackerDataInfo, ArrayMatch } from '../EasyDebug/StringTracker'; | 6 | | -import AddPlugin from '../Plugins/Index'; | | 6 | +import AddPlugin from '../Plugins/Syntax'; | 7 | 7 | import {CompileInFileFunc, StringArrayOrObject, StringAnyMap } from './XMLHelpers/CompileTypes'; | 8 | 8 | import { createNewPrint } from '../OutputInput/Logger'; | 9 | 9 | import { InsertComponentBase, BaseReader, pool } from './BaseReader/Reader'; | |
Collapse file Copy file name to clipboardExpand all lines: src/CompileCode/InsertModels.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ import { BasicSettings } from '../RunTimeBuild/SearchFileSystem'; | 3 | 3 | import { print } from '../OutputInput/Console'; | 4 | 4 | import InsertComponent from './InsertComponent'; | 5 | 5 | import { PageTemplate } from './ScriptTemplate'; | 6 | | -import AddPlugin from '../Plugins/Index'; | | 6 | +import AddPlugin from '../Plugins/Syntax'; | 7 | 7 | import { CreateFilePath, ParseDebugLine, AddDebugInfo } from './XMLHelpers/CodeInfoAndDebug'; | 8 | 8 | import * as extricate from './XMLHelpers/Extricate'; | 9 | 9 | import StringTracker from '../EasyDebug/StringTracker'; | |
Collapse file Copy file name to clipboardExpand all lines: src/MainBuild/Server.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ import { BasicSettings } from '../RunTimeBuild/SearchFileSystem'; | 8 | 8 | import formidable from 'formidable'; | 9 | 9 | import { UpdateGreenLock } from './ListenGreenLock'; | 10 | 10 | import http from 'http'; | | 11 | +import updateRequestAttributes from '../Plugins/HTTP'; | 11 | 12 |
| 12 | 13 |
| 13 | 14 | async function requestAndSettings(req: Request, res: Response) { | @@ -93,6 +94,8 @@ async function StartApp(Server?) { | 93 | 94 | * @param {Response} res - Response | 94 | 95 | */ | 95 | 96 | async function ParseRequest(req: Request, res: Response) { | | 97 | + updateRequestAttributes(req, res); | | 98 | + | 96 | 99 | if (req.method == 'POST') { | 97 | 100 | if (req.headers['content-type']?.startsWith?.('application/json')) { | 98 | 101 | Settings.middleware.bodyParser(req, res, () => requestAndSettings(req, res)); | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Request, Response } from "../MainBuild/Types"; | | 2 | +import updateResponse from "./HTTP/Response"; | | 3 | + | | 4 | +export default function updateRequestAttributes(req: Request, res: Response){ | | 5 | + updateResponse(res); | | 6 | +} |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Response } from "../../MainBuild/Types"; | | 2 | + | | 3 | +function json(data: any){ | | 4 | + this.setHeader('Content-Type', 'application/json'); | | 5 | + this.end(JSON.stringify(data)); | | 6 | + return this | | 7 | +} | | 8 | + | | 9 | +export default function updateResponse(response: Response) { | | 10 | + response.json = json; | | 11 | +} |
Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboardExpand all lines: src/RunTimeBuild/ApiCall.ts +20-32Lines changed: 20 additions & 32 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -213,6 +213,7 @@ async function makeDefinition(obj: any, urlFrom: string, defineObject: any, Requ | 213 | 213 | */ | 214 | 214 | async function MakeCall(fileModule: any, Request: any, Response: any, urlFrom: string, isDebug: boolean, nextPrase: () => Promise<any>) { | 215 | 215 | const allowErrorInfo = !GetPlugin("SafeDebug") && isDebug, makeMassage = (e: any) => (isDebug ? print.error(e) : null) + (allowErrorInfo ? `, message: ${e.message}` : ''); | | 216 | + | 216 | 217 | const method = Request.method; | 217 | 218 | let methodObj = fileModule[method] || fileModule.default[method]; //Loading the module by method | 218 | 219 | let haveMethod = true; | @@ -223,42 +224,29 @@ async function MakeCall(fileModule: any, Request: any, Response: any, urlFrom: s | 223 | 224 | } | 224 | 225 |
| 225 | 226 | const baseMethod = methodObj; | 226 | | - | 227 | 227 | const defineObject = {}; | 228 | 228 |
| 229 | | - const dataDefine = await makeDefinition(methodObj, urlFrom, defineObject, Request, Response, makeMassage); // root level definition | 230 | | - if((<any>dataDefine).error) return Response.json(dataDefine); | 231 | | - urlFrom = <string>dataDefine; | | 229 | + let nestedURL: string; | 232 | 230 |
| 233 | | - let nestedURL = findBestUrlObject(methodObj, urlFrom); | 234 | | - | 235 | | - //parse the url path | 236 | | - for(let i = 0; i< 2; i++){ | 237 | | - while ((nestedURL = findBestUrlObject(methodObj, urlFrom))) { | 238 | | - const dataDefine = await makeDefinition(methodObj, urlFrom, defineObject, Request, Response, makeMassage); | 239 | | - if((<any>dataDefine).error) return Response.json(dataDefine); | 240 | | - urlFrom = <string>dataDefine; | 241 | | - | 242 | | - urlFrom = trimType('/', urlFrom.substring(nestedURL.length)); | | 231 | + do { //parse the url path | | 232 | + if(nestedURL){ | 243 | 233 | methodObj = methodObj[nestedURL]; | | 234 | + urlFrom = trimType('/', urlFrom.substring(nestedURL.length)); | 244 | 235 | } | | 236 | + | | 237 | + const dataDefine = await makeDefinition(methodObj, urlFrom, defineObject, Request, Response, makeMassage); | | 238 | + if((<any>dataDefine).error) return Response.json(dataDefine); | 245 | 239 |
| 246 | | - if(!haveMethod){ // check if that a method | 247 | | - haveMethod = true; | 248 | | - methodObj = methodObj[method]; | 249 | | - } | 250 | | - } | | 240 | + urlFrom = <string>dataDefine; | | 241 | + } while(nestedURL = findBestUrlObject(methodObj, urlFrom)); | 251 | 242 |
| 252 | 243 | methodObj = methodObj?.func && methodObj || baseMethod; // if there is an 'any' method | 253 | | - | 254 | | - | 255 | 244 | if (!methodObj?.func) | 256 | 245 | return false; | 257 | 246 |
| 258 | | - const leftData = urlFrom.split('/'); | | 247 | + const leftData = urlFrom ? urlFrom.split('/'): []; | 259 | 248 | const urlData = []; | 260 | 249 |
| 261 | | - | 262 | 250 | let error: string; | 263 | 251 | if (methodObj.validateURL) { | 264 | 252 | for (const [index, validate] of Object.entries(methodObj.validateURL)) { | @@ -274,6 +262,7 @@ async function MakeCall(fileModule: any, Request: any, Response: any, urlFrom: s | 274 | 262 | } else | 275 | 263 | urlData.push(...leftData); | 276 | 264 |
| | 265 | + | 277 | 266 | if (!error && methodObj.validateFunc) { | 278 | 267 | let validate: any; | 279 | 268 | try { | @@ -292,26 +281,25 @@ async function MakeCall(fileModule: any, Request: any, Response: any, urlFrom: s | 292 | 281 | return Response.json({ error }); | 293 | 282 |
| 294 | 283 | const finalStep = await nextPrase(); // parse data from methods - post, get... + cookies, session... | 295 | | - | 296 | | - let apiResponse: any, newResponse: any; | | 284 | + let apiResponse: any; | 297 | 285 | try { | 298 | 286 | apiResponse = await methodObj.func(Request, Response, urlData, defineObject, leftData); | 299 | 287 | } catch (e) { | 300 | 288 | if (allowErrorInfo) | 301 | | - newResponse = { error: e.message } | | 289 | + apiResponse = { error: e.message } | 302 | 290 | else | 303 | | - newResponse = { error: '500 - Internal Server Error' }; | | 291 | + apiResponse = { error: '500 - Internal Server Error' }; | 304 | 292 | } | 305 | 293 |
| 306 | 294 | if (typeof apiResponse == 'string') | 307 | | - newResponse = { text: apiResponse }; | 308 | | - else | 309 | | - newResponse = apiResponse || newResponse; | | 295 | + apiResponse = { text: apiResponse }; | 310 | 296 |
| 311 | 297 | finalStep(); // save cookies + code | 312 | 298 |
| 313 | | - if (newResponse != null) | 314 | | - Response.json(newResponse); | | 299 | + if (apiResponse != null) // the response handled on the method | | 300 | + Response.json(apiResponse); | | 301 | + else if(!Response.finished) // the response is already sent | | 302 | + Response.end() | 315 | 303 |
| 316 | 304 | return true; | 317 | 305 | } |
Collapse file Copy file name to clipboard +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import {SearchRecord} from '@eas-framework/server' | | 1 | +import { SearchRecord } from '@eas-framework/server' | 2 | 2 |
| 3 | 3 | const docSearch = new SearchRecord('records/search.serv') | 4 | 4 | await docSearch.load() | 5 | 5 |
| | 6 | +const mapResult = (d: any[]) => d.map(x => ({ text: x.text, link: x.link })) | | 7 | + | 6 | 8 | export default { | 7 | 9 | GET: { | 8 | 10 | define: { | 9 | 11 | query: String | 10 | 12 | }, | 11 | | - func (Request, Response, _, {query}){ | 12 | | - return docSearch.search(query, {length: 15}).map(x => ({text: x.text, link: x.link})) | | 13 | + count: { | | 14 | + define: { | | 15 | + number: Number | | 16 | + }, | | 17 | + func(Request, Response, _, { query, number }) { | | 18 | + return Math.min(number, mapResult(docSearch.search(query, { length: 15 })).length) | | 19 | + } | | 20 | + }, | | 21 | + first: { | | 22 | + func(Request, Response, _, { query }) { | | 23 | + return mapResult(docSearch.search(query, { length: 15 })).pop() || 'Not found' | | 24 | + } | | 25 | + }, | | 26 | + func(Request, Response, _, { query }) { | | 27 | + return mapResult(docSearch.search(query, { length: 15 })) | 13 | 28 | } | 14 | 29 | } | 15 | 30 | } |
|