|
| 1 | +import { kvToRecord } from "../../common/util"; |
| 2 | +import { ConfigToType, DataSourcePlugin, PluginContext } from "openblocks-sdk/dataSource"; |
| 3 | +import { graphQLQueryConfig } from "../graphql/queryConfig"; |
| 4 | +import { runGraphQL } from "../graphql/run"; |
| 5 | + |
| 6 | +export const dataSourceConfig = { |
| 7 | + type: "dataSource", |
| 8 | + params: [ |
| 9 | + { |
| 10 | + type: "select", |
| 11 | + label: "Region", |
| 12 | + key: "region", |
| 13 | + options: [ |
| 14 | + { label: "United States (US)", value: "us" }, |
| 15 | + { label: "Europe (EU)", value: "eu" }, |
| 16 | + { label: "Classic", value: "classic" }, |
| 17 | + { label: "Preview", value: "preview" }, |
| 18 | + ], |
| 19 | + defaultValue: "us", |
| 20 | + tooltip: |
| 21 | + "[Document](https://docs.fauna.com/fauna/current/api/graphql/endpoints) on that you can get more information for GraphQL endpoints of Fauna.", |
| 22 | + }, |
| 23 | + { |
| 24 | + type: "password", |
| 25 | + label: "Account Key", |
| 26 | + key: "apiKey", |
| 27 | + rules: [{ required: true }], |
| 28 | + }, |
| 29 | + ], |
| 30 | +} as const; |
| 31 | + |
| 32 | +const endpoints = { |
| 33 | + us: "https://graphql.us.fauna.com/graphql", |
| 34 | + es: "https://graphql.eu.fauna.com/graphql", |
| 35 | + classic: "https://graphql.fauna.com/graphql", |
| 36 | + preview: "https://graphql.fauna-preview.com/graphql", |
| 37 | +}; |
| 38 | + |
| 39 | +const faunaPlugin: DataSourcePlugin< |
| 40 | + ConfigToType<typeof graphQLQueryConfig>, |
| 41 | + ConfigToType<typeof dataSourceConfig> |
| 42 | +> = { |
| 43 | + id: "fauna", |
| 44 | + name: "Fauna", |
| 45 | + category: "api", |
| 46 | + icon: "fauna.svg", |
| 47 | + dataSourceConfig, |
| 48 | + queryConfig: graphQLQueryConfig, |
| 49 | + run: function (actionData, dataSourceConfig, context: PluginContext): Promise<any> { |
| 50 | + const { region, apiKey } = dataSourceConfig; |
| 51 | + const { query, variables: variableKvs, headers: queryHeaders } = actionData; |
| 52 | + const headers = { |
| 53 | + ...kvToRecord(queryHeaders), |
| 54 | + Authorization: `Bearer ${apiKey}`, |
| 55 | + }; |
| 56 | + const variables = kvToRecord(variableKvs); |
| 57 | + const endpoint = endpoints[region as keyof typeof endpoints]; |
| 58 | + return runGraphQL(endpoint, query, variables, headers); |
| 59 | + }, |
| 60 | +}; |
| 61 | + |
| 62 | +export default faunaPlugin; |
0 commit comments