Skip to content

Commit

Permalink
chore: fix after rebase, lint, format
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Lavuš committed Feb 11, 2021
1 parent d663a55 commit 9367047
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 58 deletions.
52 changes: 34 additions & 18 deletions src/client/query/providers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { readFile } from 'fs/promises';
import { join as joinPath } from 'path';

import {
// isMapDocumentNode,
MapDocumentNode,
ProfileDocumentNode,
} from '@superfaceai/ast';
import { promises as fsp } from 'fs';
import { join as joinPath } from 'path';

import {
MapInterpreter,
ProfileParameterValidator,
ProviderConfig,
ProviderInfo
ProviderInfo,
} from '../../internal/interpreter';
import { NonPrimitive } from '../../internal/interpreter/variables';
import { isFileURIString, loadSuperJSON, SuperJSONDocument } from '../../internal/superjson';
import {
isFileURIString,
loadSuperJSON,
SuperJSONDocument,
} from '../../internal/superjson';
import { err, ok, Result } from '../../lib';
// import { fetchMapAST } from './registry';

Expand Down Expand Up @@ -82,10 +85,9 @@ export class BoundProvider {
}
}


export type BindConfig = {
auth?: ProviderConfig['auth'],
service?: string
auth?: ProviderConfig['auth'];
service?: string;
};

export class Provider {
Expand Down Expand Up @@ -115,12 +117,16 @@ export class Provider {
return undefined;
} else {
// TODO: I really have no idea if this is correct
return 'file:' + joinPath(process.cwd(), 'superface', 'build', profileId) + '.supr.ast.json';
return (
'file:' +
joinPath(process.cwd(), 'superface', 'build', profileId) +
'.supr.ast.json'
);
}
}
);
if (profileAst === undefined) {
throw new Error('Invalid profile')
throw new Error('Invalid profile');
}

const providerInfo = await Provider.resolveValue(
Expand All @@ -129,7 +135,7 @@ export class Provider {
_input => undefined // TODO: take from registry
);
if (providerInfo === undefined) {
throw new Error('Invalid provider info')
throw new Error('Invalid provider info');
}

const mapAst = await Provider.resolveValue(
Expand All @@ -148,11 +154,18 @@ export class Provider {
}

// TODO: I really have no idea if this is correct
return 'file:' + joinPath(process.cwd(), 'superface', 'build', baseName) + '.' + providerInfo.name + variant + '.suma.ast.json';
return (
'file:' +
joinPath(process.cwd(), 'superface', 'build', baseName) +
'.' +
providerInfo.name +
variant +
'.suma.ast.json'
);
}
);
if (mapAst === undefined) {
throw new Error('Invalid map')
throw new Error('Invalid map');
}

return new BoundProvider(
Expand All @@ -169,7 +182,7 @@ export class Provider {
// TODO: Put in appropriate place
/**
* Returns the value resolved from the input.
*
*
* The recognized input values are:
* * The value itself, returned straight away
* * `undefined`, returned straight away
Expand All @@ -184,13 +197,16 @@ export class Provider {
if (typeof input === 'string') {
if (isFileURIString(input)) {
// read in files
return parseFile(await readFile(input.slice('file:'.length), { encoding: 'utf-8' }))
} else if (false) {
return parseFile(
await fsp.readFile(input.slice('file:'.length), { encoding: 'utf-8' })
);
} else if (false) { // eslint-disable-line no-constant-condition
// TODO: detect remote url and fetch it, or call a callback
} else {
// unpack nested and recursively process them
const nested = unpackNested(input)
return Provider.resolveValue(nested, parseFile, unpackNested)
const nested = unpackNested(input);

return Provider.resolveValue(nested, parseFile, unpackNested);
}
} else {
// return undefined and T
Expand Down
2 changes: 1 addition & 1 deletion src/internal/interpreter/map-interpreter.errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('MapInterpreter errors', () => {
const providerInfo = {
name: 'test',
services: [],
defaultService: 'default'
defaultService: 'default',
};

beforeEach(async () => {
Expand Down
70 changes: 55 additions & 15 deletions src/internal/interpreter/map-interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('MapInterpreter', () => {
const providerInfo = {
name: 'test',
services: [],
defaultService: 'default'
defaultService: 'default',
};

beforeEach(async () => {
Expand Down Expand Up @@ -443,17 +443,17 @@ describe('MapInterpreter', () => {
services: [
{
id: 'default',
baseUrl: 'override me'
}
]
baseUrl: 'override me',
},
],
},
superJson: {
providers: {
test: {
services: [
{
id: 'default',
baseUrl
baseUrl,
},
],
},
Expand Down Expand Up @@ -1581,7 +1581,11 @@ describe('MapInterpreter', () => {
it('should correctly return from operation', async () => {
await mockServer.get('/test').thenJson(200, {});
const url = mockServer.urlFor('/test');
const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const ast: MapDocumentNode = {
kind: 'MapDocument',
header,
Expand Down Expand Up @@ -1735,7 +1739,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'test' });
const interpreter = new MapInterpreter({
usecase: 'test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);
expect(result.isOk() && result.value).toEqual({ answer: 42 });
});
Expand Down Expand Up @@ -1814,7 +1822,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'test' });
const interpreter = new MapInterpreter({
usecase: 'test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);
expect(result.isOk() && result.value).toEqual({ a: 41, b: 42 });
});
Expand Down Expand Up @@ -1965,7 +1977,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);
expect(result.isOk() && result.value).toEqual({
f: { a: 41 },
Expand Down Expand Up @@ -2058,7 +2074,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'test' });
const interpreter = new MapInterpreter({
usecase: 'test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);
expect(result.isOk() && result.value).toEqual({ answer: { a: 42 } });
});
Expand Down Expand Up @@ -2140,7 +2160,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);
if (result.isErr()) {
console.log(result.error);
Expand Down Expand Up @@ -2258,7 +2282,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);

expect(result.isOk() && result.value).toEqual({ results: ['Z', 'Y', 'X'] });
Expand Down Expand Up @@ -2356,7 +2384,11 @@ describe('MapInterpreter', () => {
],
};

const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);

expect(result.isOk() && result.value).toEqual({ results: ['Z', 'Y', 'X'] });
Expand Down Expand Up @@ -2479,7 +2511,11 @@ describe('MapInterpreter', () => {
},
],
};
const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);

expect(result.isOk() && result.value).toEqual({ results: ['X'] });
Expand Down Expand Up @@ -2584,7 +2620,11 @@ describe('MapInterpreter', () => {
],
};

const interpreter = new MapInterpreter({ usecase: 'Test' });
const interpreter = new MapInterpreter({
usecase: 'Test',
provider: providerInfo,
serviceId: 'default',
});
const result = await interpreter.perform(ast);

expect(result.isOk() && result.value).toEqual({ results: [2, 6] });
Expand Down
20 changes: 11 additions & 9 deletions src/internal/interpreter/map-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ function hasIteration<T extends CallStatementNode | InlineCallNode>(

// TODO: Probably deserves own module and zod parser?
export type ProviderInfo = {
name: string,
name: string;
services: {
id: string,
baseUrl: string
}[],
defaultService: string
id: string;
baseUrl: string;
}[];
defaultService: string;
};
export type ProviderConfig = {
auth?: Auth;
Expand Down Expand Up @@ -297,7 +297,8 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
security: request?.security,
auth:
this.parameters.config?.auth ??
this.parameters.superJson?.providers?.[this.parameters.provider.name].auth,
this.parameters.superJson?.providers?.[this.parameters.provider.name]
.auth,
});

for (const [handler] of responseHandlers) {
Expand Down Expand Up @@ -711,9 +712,10 @@ export class MapInterpreter<TInput extends NonPrimitive | undefined>
private get baseUrl(): string | undefined {
const prov = this.parameters.provider;

const superOverride = this.parameters.superJson?.providers?.[prov.name].services?.find(
service => service.id === this.parameters.serviceId
)?.baseUrl;
const superOverride = this.parameters.superJson?.providers?.[
prov.name
].services?.find(service => service.id === this.parameters.serviceId)
?.baseUrl;
if (superOverride !== undefined) {
return superOverride;
}
Expand Down
Loading

0 comments on commit 9367047

Please sign in to comment.