Skip to content

Commit

Permalink
feat: Add TEMPORARY TERRIBLE functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-valenta committed Jul 31, 2020
1 parent e30a7f8 commit 492be03
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 32 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
61 changes: 61 additions & 0 deletions src/client/Provider.ts
Original file line number Diff line number Diff line change
@@ -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 = <TResult>(
_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<TResult, TError>(
usecase: string,
input?: Variables
): Promise<Result<TResult, TError>> {
try {
const interpreter = new MapInterpereter({
usecase,
auth: this.config.auth,
input,
});

const result = await interpreter.visit(this.mapAST);
const mappedResult = mapResult<TResult>(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<BoundProvider> {
if (!mapAST) {
throw new Error('Method not implemented');
}

return new BoundProvider(this.config, this.profileAST, mapAST);
}
}
18 changes: 18 additions & 0 deletions src/client/SuperfaceClient.ts
Original file line number Diff line number Diff line change
@@ -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<Provider[]> {
if (!query.ast) {
throw new Error('Method not implemented.');
}

return [new Provider(this.config, query.ast.profileAST)];
}
}
2 changes: 1 addition & 1 deletion src/client/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Config {
credentials?: {
auth?: {
basic?: {
username: string;
password: string;
Expand Down
3 changes: 2 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './config';
export * from './interfaces';
export * from './query';
export * from './Provider';
export * from './SuperfaceClient';
21 changes: 0 additions & 21 deletions src/client/interfaces.ts

This file was deleted.

14 changes: 12 additions & 2 deletions src/client/query/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ProfileDocumentNode } from '@superindustries/language';

interface QueryNumber {
lt?: number;
gt?: number;
Expand All @@ -17,10 +19,18 @@ interface QueryResultProperty {
present?: boolean;
}

interface QueryAST {
profileAST: ProfileDocumentNode;
}

type QueryParameter =
| QueryNumber
| QueryString
| QueryInputParameter
| QueryResultProperty;
| QueryResultProperty
| QueryAST;

export type Query = Record<string, QueryParameter>;
export type Query = Record<string, QueryParameter> & {
// TODO: It's fake!
ast?: QueryAST;
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './client';
export * from './interfaces';
// export * from './interfaces';
export * from './lib';
2 changes: 2 additions & 0 deletions src/internal/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export const HttpClient = {
body = await response.text();
}

console.log(body);

const responseHeaders: Record<string, string> = {};
response.headers.forEach((key, value) => {
responseHeaders[key] = value;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": [],
"module": "ESNext",
"module": "CommonJS",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
Expand Down

0 comments on commit 492be03

Please sign in to comment.