"use strict";
(() => {
var exports = {};
exports.id = 702;
exports.ids = [702];
exports.modules = {

/***/ 6352:
/***/ ((module) => {

module.exports = require("apollo-server-micro");

/***/ }),

/***/ 2167:
/***/ ((module) => {

module.exports = require("axios");

/***/ }),

/***/ 6762:
/***/ ((module) => {

module.exports = require("graphql-type-json");

/***/ }),

/***/ 9344:
/***/ ((module) => {

module.exports = require("jsonwebtoken");

/***/ }),

/***/ 2799:
/***/ ((module) => {

module.exports = require("micro-cors");

/***/ }),

/***/ 4558:
/***/ ((module) => {

module.exports = require("next/config");

/***/ }),

/***/ 6555:
/***/ ((module) => {

module.exports = import("uuid");;

/***/ }),

/***/ 7147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 3816:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "config": () => (/* binding */ config),
/* harmony export */   "default": () => (__WEBPACK_DEFAULT_EXPORT__)
/* harmony export */ });
/* harmony import */ var apollo_server_micro__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6352);
/* harmony import */ var apollo_server_micro__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(apollo_server_micro__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var graphql_type_json__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6762);
/* harmony import */ var graphql_type_json__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(graphql_type_json__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _vulcan_sql_catalog_server_utils_authHelper__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(1000);
/* harmony import */ var _vulcan_sql_catalog_server_utils_vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4216);
/* harmony import */ var _vulcan_sql_catalog_server_utils_dataModel__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(9243);
/* harmony import */ var micro_cors__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(2799);
/* harmony import */ var micro_cors__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(micro_cors__WEBPACK_IMPORTED_MODULE_5__);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([_vulcan_sql_catalog_server_utils_authHelper__WEBPACK_IMPORTED_MODULE_2__]);
_vulcan_sql_catalog_server_utils_authHelper__WEBPACK_IMPORTED_MODULE_2__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];






const cors = micro_cors__WEBPACK_IMPORTED_MODULE_5__();
const typeDefs = apollo_server_micro__WEBPACK_IMPORTED_MODULE_0__.gql`
  scalar JSON

  enum ColumnType {
    STRING
    NUMBER
    DATE
    DATETIME
    BOOLEAN
  }

  type Parameter {
    name: String!
    key: String!
    type: ColumnType!
    description: String
    required: Boolean!
  }

  type Column {
    name: String!
    type: ColumnType!
    description: String
  }

  type Endpoint {
    slug: String!
    name: String!
    description: String
    apiDocUrl: String!
    parameters: [Parameter]!
    columns: [Column]!
  }

  type DatasetMetadata {
    currentCount: Int!
    totalCount: Int!
  }

  type Dataset {
    data: JSON!
    metadata: DatasetMetadata!
    apiUrl: String!
    csvDownloadUrl: String
    jsonDownloadUrl: String
    shareJsonUrl: String!
  }

  type Query {
    endpoint(slug: String): Endpoint!
    endpoints: [Endpoint]!
    dataset(endpointSlug: String, filter: JSON): Dataset!
  }
`;
const resolvers = {
    JSON: (graphql_type_json__WEBPACK_IMPORTED_MODULE_1___default()),
    Query: {
        endpoints: async (_, args, ctx)=>{
            const schemas = await _vulcan_sql_catalog_server_utils_vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_3__/* ["default"].getSchemas */ .Z.getSchemas(ctx);
            return schemas.map((schema)=>new _vulcan_sql_catalog_server_utils_dataModel__WEBPACK_IMPORTED_MODULE_4__/* .Endpoint */ .F(schema)
            );
        },
        endpoint: async (_, args, ctx)=>{
            const schema = await _vulcan_sql_catalog_server_utils_vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_3__/* ["default"].getSchema */ .Z.getSchema(ctx, args.slug);
            return new _vulcan_sql_catalog_server_utils_dataModel__WEBPACK_IMPORTED_MODULE_4__/* .Endpoint */ .F(schema);
        },
        dataset: async (_, args, ctx)=>{
            const { schema , data  } = await _vulcan_sql_catalog_server_utils_vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_3__/* ["default"].getPreviewData */ .Z.getPreviewData(ctx, {
                slug: args.endpointSlug,
                filter: args.filter
            });
            return new _vulcan_sql_catalog_server_utils_dataModel__WEBPACK_IMPORTED_MODULE_4__/* .Dataset */ .q(schema, data);
        }
    }
};
const apolloServer = new apollo_server_micro__WEBPACK_IMPORTED_MODULE_0__.ApolloServer({
    typeDefs,
    resolvers,
    context: async ({ req  })=>{
        const accessToken = (0,_vulcan_sql_catalog_server_utils_authHelper__WEBPACK_IMPORTED_MODULE_2__/* .getBearerToken */ .Oj)(req.headers);
        const { profile , apiToken  } = await _vulcan_sql_catalog_server_utils_authHelper__WEBPACK_IMPORTED_MODULE_2__/* .authHelper.auth */ .$K.auth(accessToken);
        return {
            profile,
            apiToken
        };
    }
});
const startServer = apolloServer.start();
const handler = async (req, res)=>{
    await startServer;
    await apolloServer.createHandler({
        path: "/api/graphql"
    })(req, res);
};
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (cors((req, res)=>req.method === "OPTIONS" ? res.status(200).end() : handler(req, res)
));
const config = {
    api: {
        bodyParser: false
    }
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 9243:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "F": () => (/* binding */ Endpoint),
/* harmony export */   "q": () => (/* binding */ Dataset)
/* harmony export */ });
/* harmony import */ var _errorCode__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5373);

class Endpoint {
    constructor(schema){
        this.schema = schema;
    }
    get slug() {
        return Buffer.from(this.schema.urlPath).toString("base64");
    }
    get name() {
        return this.schema.urlPath.replace("/", "");
    }
    get description() {
        return this.schema.description;
    }
    get apiDocUrl() {
        return this.schema.apiDocUrl;
    }
    get parameters() {
        return (this.schema.request || []).map((param)=>{
            return {
                name: param.fieldName,
                type: param.type,
                key: param.fieldName,
                description: param.description,
                required: param.validators.some((validator)=>validator.name === "required"
                )
            };
        });
    }
    get columns() {
        return (this.schema.response || []).map((column)=>{
            return {
                name: column.name,
                type: column.type,
                description: column.description || "",
                required: column.required || false
            };
        });
    }
}
class Dataset {
    constructor(schema, originalData){
        this.schema = schema;
        this.originalData = originalData;
        if (!Array.isArray(this.originalData)) {
            throw _errorCode__WEBPACK_IMPORTED_MODULE_0__/* .errorCode.RESPONSE_FORMAT_ERROR */ .u.RESPONSE_FORMAT_ERROR;
        }
        // get the first 100 rows
        this.data = this.originalData.slice(0, 100);
    }
    get metadata() {
        return {
            totalCount: this.originalData.length,
            currentCount: this.data.length
        };
    }
    get apiUrl() {
        return this.schema.actualUrl;
    }
    get csvDownloadUrl() {
        const isSupportCsv = (this.schema.responseFormat.formats || []).includes("csv");
        const formatPath = this.getFormatPath(this.schema.actualPath, ".csv");
        return isSupportCsv ? `/api/download${formatPath}${this.getShareKey()}` : "";
    }
    get jsonDownloadUrl() {
        const isSupportJson = (this.schema.responseFormat.formats || []).includes("json");
        const formatPath = this.getFormatPath(this.schema.actualPath, ".json");
        return isSupportJson ? `/api/download${formatPath}${this.getShareKey()}` : "";
    }
    get shareJsonUrl() {
        const formatUrl = this.getFormatPath(this.schema.actualUrl, "");
        return `${formatUrl}${this.getShareKey()}`;
    }
    getFormatPath(urlPath, format = "") {
        const [path, search] = urlPath.split("?");
        const searchKey = search ? `?${search}` : "";
        return `${path}${format}${searchKey}`;
    }
    getShareKey() {
        const { shareKey , actualPath  } = this.schema;
        const hasQuery = actualPath.includes("?");
        return hasQuery ? shareKey.replace("?", "&") : shareKey;
    }
}


/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [216,0], () => (__webpack_exec__(3816)));
module.exports = __webpack_exports__;

})();