UNPKG

@aut-labs/sdk

Version:

The TS/JS SDK package aims to make it easy for frontends/backends to integrate with Aut Smart Contracts

101 lines 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryParams = exports.queryParamsAsString = exports.GraphQLFilter = void 0; class GraphQLFilter { constructor(filter) { this.prop = filter.prop; this.comparison = filter.comparison; this.value = filter.value; } } exports.GraphQLFilter = GraphQLFilter; const opionalChaining = (filter) => { const optionalFilters = []; const values = Array.isArray(filter.value) ? filter.value : [filter.value]; // Determine the appropriate filter format let filterSuffix = ''; // eslint-disable-next-line default-case switch (filter.comparison) { case 'oneOf': filterSuffix = '_in'; break; // Add additional cases if needed } // Compile the filter strings values.forEach((v) => { if (typeof v === 'string') { optionalFilters.push(`{${filter.prop}${filterSuffix}: "${v}"}`); } else { optionalFilters.push(`{${filter.prop}${filterSuffix}: ${v}}`); } }); // Construct the final filter string if (optionalFilters.length > 1) { return optionalFilters.join(', '); // Combines multiple filters with a comma (adjust if needed for your GraphQL implementation) } return optionalFilters[0]; }; const stringifyFilter = (filters) => { return (filters // eslint-disable-next-line array-callback-return .map((filter) => { switch (filter.comparison) { // Add cases here for specific comparisons, // based on the GraphQL schema filter patterns shared case 'equals': return `${filter.prop}: "${filter.value}"`; case 'notEquals': return `${filter.prop}_not: "${filter.value}"`; case 'gt': return `${filter.prop}_gt: "${filter.value}"`; case 'gte': return `${filter.prop}_gte: "${filter.value}"`; case 'lt': return `${filter.prop}_lt: "${filter.value}"`; case 'lte': return `${filter.prop}_lte: "${filter.value}"`; case 'contains': if (typeof filter.value === 'string') { return `${filter.prop}_contains: "${filter.value}"`; } // For array of values (oneOf) return opionalChaining(filter); case 'notContains': if (typeof filter.value === 'string') { return `${filter.prop}_not_contains: "${filter.value}"`; } // If the filter does not handle an array for notContains, omit this break; // More cases can be added as needed... default: return ''; // or throw error if unsupported comparison is given } }) .join(', ')); }; const queryParamsAsString = (params, defaultIdKey = null) => { let queryString = `skip: ${params.skip}, first: ${params.take}`; if (params.filters?.length) { // Enclose the full filter string in braces as it represents object syntax const filtersString = stringifyFilter(params.filters); queryString += `, where: {${filtersString}}`; } // Append additional query parameters as needed, like orderBy, etc. return queryString; }; exports.queryParamsAsString = queryParamsAsString; class QueryParams { constructor(params, defaultIdKey = null) { this.skip = params?.skip || 0; this.take = params?.take || 20; this.filters = params?.filters; this.filter = params?.filter; this.sort = params.sort; if (!!this.sort && !!defaultIdKey) { this.sort = { [defaultIdKey]: 'DESC' }; } } } exports.QueryParams = QueryParams; //# sourceMappingURL=graphql.misc.js.map