From 492be037543eed82be06d4eb50ed8360f0209658 Mon Sep 17 00:00:00 2001 From: Lukas Valenta Date: Fri, 31 Jul 2020 15:31:07 +0200 Subject: [PATCH] feat: Add TEMPORARY TERRIBLE functionality --- .eslintrc.js | 1 + package.json | 10 +++--- src/client/Provider.ts | 61 +++++++++++++++++++++++++++++++++++ src/client/SuperfaceClient.ts | 18 +++++++++++ src/client/config/config.ts | 2 +- src/client/index.ts | 3 +- src/client/interfaces.ts | 21 ------------ src/client/query/query.ts | 14 ++++++-- src/index.ts | 2 +- src/internal/http.ts | 2 ++ tsconfig.json | 2 +- 11 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 src/client/Provider.ts create mode 100644 src/client/SuperfaceClient.ts delete mode 100644 src/client/interfaces.ts diff --git a/.eslintrc.js b/.eslintrc.js index e0fc9fa0..4727f7c2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -31,6 +31,7 @@ module.exports = { 'no-multiple-empty-lines': 'error', 'lines-between-class-members': 'off', '@typescript-eslint/lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true, exceptAfterOverload: true }], + '@typescript-eslint/require-await': 'off', }, settings: { 'import/parsers': { diff --git a/package.json b/package.json index d2a95222..0056f2a1 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,11 @@ "name": "@superindustries/superface", "version": "0.0.1", "description": "Level 5 autonomous, self-driving API client, https://superface.ai", - "main": "dist/superface.js", + "main": "dist/index.js", "source": "src/index.ts", - "module": "dist/superface.modern.js", - "unpkg": "dist/superface.umd.js", - "browser": "dist/superface.umd.js", + "module": "dist/index.modern.js", + "unpkg": "dist/index.umd.js", + "browser": "dist/index.umd.js", "types": "dist/index.d.ts", "repository": "https://github.com/superindustries/superface.git", "author": "Superface Team", @@ -18,7 +18,7 @@ "registry": "https://npm.pkg.github.com/" }, "scripts": { - "build": "microbundle --tsconfig tsconfig.release.json", + "build": "tsc --outDir dist -p tsconfig.release.json", "watch": "yarn build --watch", "clean": "rimraf dist/", "prebuild": "yarn clean", diff --git a/src/client/Provider.ts b/src/client/Provider.ts new file mode 100644 index 00000000..78677a38 --- /dev/null +++ b/src/client/Provider.ts @@ -0,0 +1,61 @@ +import { + MapDocumentNode, + ProfileDocumentNode, +} from '@superindustries/language'; + +import { Result } from '..'; +import { Variables } from '../internal/interpreter/interfaces'; +import { MapInterpereter } from '../internal/interpreter/map-interpreter'; +import { err, ok } from '../lib'; +import { Config } from './config'; + +// TODO: Create Profile walker for validation of result +export const mapResult = ( + _profileAST: ProfileDocumentNode, + result?: string | Variables +): TResult => { + return (result as unknown) as TResult; +}; + +export class BoundProvider { + constructor( + private readonly config: Config, + private readonly profileAST: ProfileDocumentNode, + private readonly mapAST: MapDocumentNode + ) {} + + async perform( + usecase: string, + input?: Variables + ): Promise> { + try { + const interpreter = new MapInterpereter({ + usecase, + auth: this.config.auth, + input, + }); + + const result = await interpreter.visit(this.mapAST); + const mappedResult = mapResult(this.profileAST, result); + + return ok(mappedResult); + } catch (e) { + return err(e); + } + } +} + +export class Provider { + constructor( + private readonly config: Config, + private readonly profileAST: ProfileDocumentNode + ) {} + + public async bind(mapAST?: MapDocumentNode): Promise { + if (!mapAST) { + throw new Error('Method not implemented'); + } + + return new BoundProvider(this.config, this.profileAST, mapAST); + } +} diff --git a/src/client/SuperfaceClient.ts b/src/client/SuperfaceClient.ts new file mode 100644 index 00000000..693276df --- /dev/null +++ b/src/client/SuperfaceClient.ts @@ -0,0 +1,18 @@ +import { Config } from './config'; +import { Provider } from './Provider'; +import { Query } from './query'; + +export class SuperfaceClient { + constructor(private readonly config: Config) {} + + public async findProviders( + _profileIds: string | string[], + query: Query + ): Promise { + if (!query.ast) { + throw new Error('Method not implemented.'); + } + + return [new Provider(this.config, query.ast.profileAST)]; + } +} diff --git a/src/client/config/config.ts b/src/client/config/config.ts index b794836b..7b52a9af 100644 --- a/src/client/config/config.ts +++ b/src/client/config/config.ts @@ -1,5 +1,5 @@ export interface Config { - credentials?: { + auth?: { basic?: { username: string; password: string; diff --git a/src/client/index.ts b/src/client/index.ts index 83cbf584..9959e395 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -1,3 +1,4 @@ export * from './config'; -export * from './interfaces'; export * from './query'; +export * from './Provider'; +export * from './SuperfaceClient'; diff --git a/src/client/interfaces.ts b/src/client/interfaces.ts deleted file mode 100644 index c6567910..00000000 --- a/src/client/interfaces.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Result } from '..'; -import { Config } from './config'; -import { Query } from './query'; - -export interface BoundProvider { - perform( - operation: string, - params: TParams - ): Promise>; -} - -export interface Provider { - bind(config: Config): BoundProvider; -} - -export interface SuperfaceClient { - findProviders( - profileIds: string | string[], - query: Query - ): Promise; -} diff --git a/src/client/query/query.ts b/src/client/query/query.ts index 62883881..6a2a4205 100644 --- a/src/client/query/query.ts +++ b/src/client/query/query.ts @@ -1,3 +1,5 @@ +import { ProfileDocumentNode } from '@superindustries/language'; + interface QueryNumber { lt?: number; gt?: number; @@ -17,10 +19,18 @@ interface QueryResultProperty { present?: boolean; } +interface QueryAST { + profileAST: ProfileDocumentNode; +} + type QueryParameter = | QueryNumber | QueryString | QueryInputParameter - | QueryResultProperty; + | QueryResultProperty + | QueryAST; -export type Query = Record; +export type Query = Record & { + // TODO: It's fake! + ast?: QueryAST; +}; diff --git a/src/index.ts b/src/index.ts index 0ef13570..ba627dbe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export * from './client'; -export * from './interfaces'; +// export * from './interfaces'; export * from './lib'; diff --git a/src/internal/http.ts b/src/internal/http.ts index 24364940..01c7b11c 100644 --- a/src/internal/http.ts +++ b/src/internal/http.ts @@ -181,6 +181,8 @@ export const HttpClient = { body = await response.text(); } + console.log(body); + const responseHeaders: Record = {}; response.headers.forEach((key, value) => { responseHeaders[key] = value; diff --git a/tsconfig.json b/tsconfig.json index a4803380..80ce7e7c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "lib": [], - "module": "ESNext", + "module": "CommonJS", "moduleResolution": "node", "noFallthroughCasesInSwitch": true, "noImplicitReturns": true,