superfaceai / one-sdk-js

1️⃣ One Node.js SDK for all the APIs you want to integrate with
https://superface.ai
MIT License
46 stars 3 forks source link

Add ast property to superjson to load profile ast, map ast and provider.json #336

Closed freaz closed 1 year ago

freaz commented 1 year ago

Goal is to be able to pass ASTs directly in super.json. See example bellow.

Eventually I will add ast to SuperJson, but for internal use this should be sufficient.

import type { SuperJsonDocument } from '@superfaceai/ast';
import { readFile } from 'fs/promises';
import { resolve } from 'path';

import { SuperfaceClient } from '../node';

async function readAst(file: string): Promise<string> {
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
  return await readFile(resolve(__dirname, file), { encoding: 'utf8' });
}

async function main() {
  const superJson: SuperJsonDocument = {
    profiles: {
      profile: {
        ast: await readAst('./profile.supr.ast.json'),
        providers: {
          provider: {
            ast: await readAst('./map.suma.ast.json'),
          },
        },
      },
    },
    providers: {
      provider: {
        ast: await readAst('./provider.json'), // I know it is not ast, but consistency with profile and map(profile provider)
      },
    },
  } as unknown as SuperJsonDocument; // this is needed for TS to compile

  const client = new SuperfaceClient({ superJson, superfacePath: __dirname });
  const profile = await client.getProfile('profile');
  const result = await profile.getUseCase('UseCase').perform({ foo: 'foo' });

  console.log(result);
}

void main();