diff --git a/src/clients/SquidClient.ts b/src/clients/SquidClient.ts index 50e2870..c56da2e 100644 --- a/src/clients/SquidClient.ts +++ b/src/clients/SquidClient.ts @@ -1,13 +1,28 @@ - import { Prefix } from '@kodadot1/static' import { $fetch } from 'ofetch' import getUrl from '../indexers' import { getOptions } from '../indexers/utils' import build from '../queryBuilder' -import { BaseEvent, GraphLike, GraphQuery, KeyOf, ObjProp, QueryProps, SquidCollection, SquidNFT } from '../types' +import { + BaseEvent, + GraphLike, + GraphQuery, + KeyOf, + ObjProp, + QueryProps, + SquidCollection, + SquidNFT, +} from '../types' import AbstractClient from './abstractClient' -import { defaultEventField, genericCountQuery, getFields, includeBurned, optionToQuery, strOf } from './defaults' +import { + defaultEventField, + genericCountQuery, + getFields, + includeBurned, + optionToQuery, + strOf, +} from './defaults' class SquidClient implements AbstractClient { private prefix?: Prefix @@ -20,16 +35,22 @@ class SquidClient implements AbstractClient { collectionById(id: string, fields?: ObjProp): GraphQuery { const toQuery = getFields(fields) - return build('collection: collectionEntityById', toQuery, { id: { type: 'String', required: true, value: id, name: 'id' } }) + return build('collection: collectionEntityById', toQuery, { + id: { type: 'String', required: true, value: id, name: 'id' }, + }) } collectionCountByIssuer(issuer: string): GraphQuery { - const { operation, fields, variables } = genericCountQuery('collection', { issuer_eq: issuer }) + const { operation, fields, variables } = genericCountQuery('collection', { + issuer_eq: issuer, + }) return build(operation, fields, variables) } collectionCountByName(name: string): GraphQuery { - const gcq = genericCountQuery('collection', { name_containsInsensitive: name }) + const gcq = genericCountQuery('collection', { + name_containsInsensitive: name, + }) return build(gcq.operation, gcq.fields, gcq.variables) } @@ -39,37 +60,70 @@ class SquidClient implements AbstractClient { } collectionCountCreatedAfter(date: Date): GraphQuery { - const gcq = genericCountQuery('collection', { createdAt_gte: date.toISOString() }) + const gcq = genericCountQuery('collection', { + createdAt_gte: date.toISOString(), + }) return build(gcq.operation, gcq.fields, gcq.variables) } - collectionListBy(id: string, field: KeyOf, options?: QueryProps): GraphQuery { + collectionListBy( + id: string, + field: KeyOf, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) - return build(`collections: collectionEntities(where: {${field}_eq: "${id}"})`, toQuery) + return build( + `collections: collectionEntities(where: {${field}_eq: "${id}"})`, + toQuery, + ) } - collectionListByIssuer(issuer: string, options?: QueryProps): GraphQuery { + collectionListByIssuer( + issuer: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`collections: collectionEntities(where: {issuer_eq: "${issuer}"} ${optionList})`, toQuery) + return build( + `collections: collectionEntities(where: {issuer_eq: "${issuer}"} ${optionList})`, + toQuery, + ) } - collectionListByName(name: string, options?: QueryProps): GraphQuery { + collectionListByName( + name: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`collections: collectionEntities(where: {name_containsInsensitive: "${name}"} ${optionList})`, toQuery) + return build( + `collections: collectionEntities(where: {name_containsInsensitive: "${name}"} ${optionList})`, + toQuery, + ) } - collectionListByOwner(owner: string, options?: QueryProps): GraphQuery { + collectionListByOwner( + owner: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`collections: collectionEntities(where: {currentOwner_eq: "${owner}"} ${optionList})`, toQuery) + return build( + `collections: collectionEntities(where: {currentOwner_eq: "${owner}"} ${optionList})`, + toQuery, + ) } - collectionListCreatedAfter(date: Date, options?: QueryProps): GraphQuery { + collectionListCreatedAfter( + date: Date, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`collections: collectionEntities(where: {createdAt_gte: "${date.toISOString()}"} ${optionList})`, toQuery) + return build( + `collections: collectionEntities(where: {createdAt_gte: "${date.toISOString()}"} ${optionList})`, + toQuery, + ) } eventList(options?: QueryProps): GraphQuery { @@ -78,34 +132,102 @@ class SquidClient implements AbstractClient { return build(`events(${optionList})`, toQuery) } - eventListByAddress(address: string, options?: QueryProps): GraphQuery { + eventListByAddress( + address: string, + options?: QueryProps, + ): GraphQuery { + const toQuery = getFields(options?.fields, defaultEventField, false) + const optionList = optionToQuery(options, true) + return build( + `events(where: {caller_eq: "${address}" } ${optionList})`, + toQuery, + ) + } + + eventListByCollectionId( + id: string, + options?: QueryProps, + ): GraphQuery { + const toQuery = getFields(options?.fields, defaultEventField, false) + const optionList = optionToQuery(options, true) + return build( + `events(where: {nft: { collection: {id_eq: "${id}"}}} ${optionList})`, + toQuery, + ) + } + + eventListByCollectionIdAndInteraction( + id: string, + interaction: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields, defaultEventField, false) const optionList = optionToQuery(options, true) - return build(`events(where: {caller_eq: "${address}" } ${optionList})`, toQuery) + return build( + `events(where: {nft: { collection: {id_eq: "${id}"}} interaction_eq: ${interaction} } ${optionList})`, + toQuery, + ) } - eventListByCollectionId(id: string, options?: QueryProps): GraphQuery { + eventListByCollectionIdAndInteractionList( + id: string, + interactions: string[], + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields, defaultEventField, false) const optionList = optionToQuery(options, true) - return build(`events(where: {nft: {id_eq: "${id}"}} ${optionList})`, toQuery) - // events(where: { nft: { collection: { id_eq: "" }}}) + const list = JSON.stringify(interactions).replace(/"/g, '') + return build( + `events(where: {nft: { collection: {id_eq: "${id}"}} interaction_in: ${list} } ${optionList})`, + toQuery, + ) } - eventListByInteraction(interaction: string, options?: QueryProps): GraphQuery { + eventListByInteraction( + interaction: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields, defaultEventField, false) const optionList = optionToQuery(options, true) - return build(`events(where: {interaction_eq: ${interaction}} ${optionList})`, toQuery) + return build( + `events(where: {interaction_eq: ${interaction}} ${optionList})`, + toQuery, + ) } eventListByItemId(id: string, options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields, defaultEventField, false) const optionList = optionToQuery(options, true) - return build(`events(where: {nft: {id_eq: "${id}"}} ${optionList})`, toQuery) + return build( + `events(where: {nft: {id_eq: "${id}"}} ${optionList})`, + toQuery, + ) + } + + eventListByItemIdAndInteraction(id: string, interaction: string, options?: QueryProps): GraphQuery { + const toQuery = getFields(options?.fields, defaultEventField, false) + const optionList = optionToQuery(options, true) + return build( + `events(where: {nft: {id_eq: "${id}" } interaction_eq: ${interaction} } ${optionList})`, + toQuery, + ) + } + + eventListByItemIdAndInteractionList(id: string, interactions: string[], options?: QueryProps): GraphQuery { + const toQuery = getFields(options?.fields, defaultEventField, false) + const optionList = optionToQuery(options, true) + const list = JSON.stringify(interactions).replace(/"/g, '') + return build( + `events(where: {nft: {id_eq: "${id}" } interaction_in: ${list} } ${optionList})`, + toQuery, + ) } itemById(id: string, fields?: ObjProp): GraphQuery { const toQuery = getFields(fields) - return build('item: nftEntityById', toQuery, { id: { type: 'String', required: true, value: id, name: 'id' } }) + return build('item: nftEntityById', toQuery, { + id: { type: 'String', required: true, value: id, name: 'id' }, + }) } itemCountByOwner(owner: string): GraphQuery { @@ -114,7 +236,9 @@ class SquidClient implements AbstractClient { } itemCountByIssuer(issuer: string): GraphQuery { - const { operation, fields, variables } = genericCountQuery('item', { issuer_eq: issuer }) + const { operation, fields, variables } = genericCountQuery('item', { + issuer_eq: issuer, + }) return build(operation, fields, variables) } @@ -124,12 +248,18 @@ class SquidClient implements AbstractClient { } itemCountCollectedBy(address: string): GraphQuery { - const gcq = genericCountQuery('item', { currentOwner_eq: address, issuer_not_eq: address }) + const gcq = genericCountQuery('item', { + currentOwner_eq: address, + issuer_not_eq: address, + }) return build(gcq.operation, gcq.fields, gcq.variables) } itemCountSoldBy(address: string): GraphQuery { - const gcq = genericCountQuery('item', { currentOwner_not_eq: address, issuer_eq: address }) + const gcq = genericCountQuery('item', { + currentOwner_not_eq: address, + issuer_eq: address, + }) return build(gcq.operation, gcq.fields, gcq.variables) } @@ -152,7 +282,10 @@ class SquidClient implements AbstractClient { itemCountByCollectionIdAndOwner(id: string, owner: string): GraphQuery { const value = { id_eq: id } - const gcq = genericCountQuery('item', { collection: value, currentOwner_eq: owner }) + const gcq = genericCountQuery('item', { + collection: value, + currentOwner_eq: owner, + }) return build(gcq.operation, gcq.fields, gcq.variables) } @@ -165,88 +298,150 @@ class SquidClient implements AbstractClient { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) const burned = includeBurned(options) - return build(`items: nftEntities(where: {currentOwner_eq: "${owner}", ${burned}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {currentOwner_eq: "${owner}", ${burned}} ${optionList})`, + toQuery, + ) } itemListByIssuer(issuer: string, options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) const burned = includeBurned(options) - return build(`items: nftEntities(where: {issuer_eq: "${issuer}", ${burned}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {issuer_eq: "${issuer}", ${burned}} ${optionList})`, + toQuery, + ) } itemListByName(name: string, options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) const burned = includeBurned(options) - return build(`items: nftEntities(where: {name_containsInsensitive: "${name}", ${burned}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {name_containsInsensitive: "${name}", ${burned}} ${optionList})`, + toQuery, + ) } - itemListCollectedBy(address: string, options?: QueryProps): GraphQuery { + itemListCollectedBy( + address: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) const burned = includeBurned(options) - return build(`items: nftEntities(where: {currentOwner_eq: "${address}", issuer_not_eq: "${address}", ${burned}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {currentOwner_eq: "${address}", issuer_not_eq: "${address}", ${burned}} ${optionList})`, + toQuery, + ) } itemListSoldBy(address: string, options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {currentOwner_not_eq: "${address}", issuer_eq: "${address}"} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {currentOwner_not_eq: "${address}", issuer_eq: "${address}"} ${optionList})`, + toQuery, + ) } - itemListByCollectionId(id: string, options?: QueryProps): GraphQuery { + itemListByCollectionId( + id: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {collection: {id_eq: "${id}"}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {collection: {id_eq: "${id}"}} ${optionList})`, + toQuery, + ) } itemListForSale(options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {price_gt: "0"} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {price_gt: "0"} ${optionList})`, + toQuery, + ) } - itemListForSaleByCollectionId(id: string, options?: QueryProps): GraphQuery { + itemListForSaleByCollectionId( + id: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {collection: {id_eq: "${id}"}, price_gt: "0"} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {collection: {id_eq: "${id}"}, price_gt: "0"} ${optionList})`, + toQuery, + ) } - itemListBy(id: string, field: KeyOf, options?: QueryProps): GraphQuery { + itemListBy( + id: string, + field: KeyOf, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) return build(`items: nftEntities(where: {${field}_eq: "${id}"})`, toQuery) } - itemListByCollectionIdAndOwner(id: string, owner: string, options?: QueryProps): GraphQuery { + itemListByCollectionIdAndOwner( + id: string, + owner: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: { currentOwner_eq: "${owner}", collection: {id_eq: "${id}"}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: { currentOwner_eq: "${owner}", collection: {id_eq: "${id}"}} ${optionList})`, + toQuery, + ) } - itemListByCollectionIdList(ids: string[], options?: QueryProps): GraphQuery { + itemListByCollectionIdList( + ids: string[], + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) const list = JSON.stringify(ids) - return build(`items: nftEntities(where: {collection: {id_in: ${list}}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {collection: {id_in: ${list}}} ${optionList})`, + toQuery, + ) } itemListByMetadataId(id: string, options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {meta: {id_eq: "${id}"}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {meta: {id_eq: "${id}"}} ${optionList})`, + toQuery, + ) } - itemListByMetadataIdMatch(id: string, options?: QueryProps): GraphQuery { + itemListByMetadataIdMatch( + id: string, + options?: QueryProps, + ): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {meta: {id_containsInsensitive: "${id}"}} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {meta: {id_containsInsensitive: "${id}"}} ${optionList})`, + toQuery, + ) } itemListCreatedAfter(date: Date, options?: QueryProps): GraphQuery { const toQuery = getFields(options?.fields) const optionList = optionToQuery(options, true) - return build(`items: nftEntities(where: {createdAt_gte: "${date.toISOString()}"} ${optionList})`, toQuery) + return build( + `items: nftEntities(where: {createdAt_gte: "${date.toISOString()}"} ${optionList})`, + toQuery, + ) } fetch(query: GraphQuery): Promise> { diff --git a/src/clients/abstractClient.ts b/src/clients/abstractClient.ts index 6a3348b..7b8c48e 100644 --- a/src/clients/abstractClient.ts +++ b/src/clients/abstractClient.ts @@ -3,10 +3,10 @@ import { BaseEvent, GraphLike, GraphQuery, ObjProp, QueryProps } from '../types' interface AbstractClient { collectionById(id: string, fields?: ObjProp): GraphQuery // collectionListBy(id: string, field: KeyOf, fields?: ObjProp): GraphQuery - collectionCountByIssuer(issuer: string): GraphQuery ; - collectionCountByName(name: string): GraphQuery ; - collectionCountByOwner(owner: string): GraphQuery ; - collectionCountCreatedAfter(date: Date): GraphQuery ; + collectionCountByIssuer(issuer: string): GraphQuery + collectionCountByName(name: string): GraphQuery + collectionCountByOwner(owner: string): GraphQuery + collectionCountCreatedAfter(date: Date): GraphQuery collectionListByIssuer(issuer: string, options?: QueryProps): GraphQuery collectionListByName(name: string, options?: QueryProps): GraphQuery collectionListByOwner(owner: string, options?: QueryProps): GraphQuery @@ -15,23 +15,41 @@ interface AbstractClient { eventList(options?: QueryProps): GraphQuery eventListByAddress(address: string, options?: QueryProps): GraphQuery eventListByCollectionId(id: string, options?: QueryProps): GraphQuery - // eventListByCollectionIdAndInteraction(id: string, interaction: string, options?: QueryProps): GraphQuery - eventListByInteraction(interaction: string, options?: QueryProps): GraphQuery + eventListByCollectionIdAndInteraction(id: string, interaction: string, options?: QueryProps): GraphQuery + eventListByCollectionIdAndInteractionList(id: string, interactions: string[], options?: QueryProps): GraphQuery + eventListByInteraction( + interaction: string, + options?: QueryProps, + ): GraphQuery eventListByItemId(id: string, options?: QueryProps): GraphQuery + eventListByItemIdAndInteraction( + id: string, + interaction: string, + options?: QueryProps, + ): GraphQuery + eventListByItemIdAndInteractionList( + id: string, + interactions: string[], + options?: QueryProps, + ): GraphQuery itemById(id: string, fields?: ObjProp): GraphQuery - itemCountByOwner(owner: string): GraphQuery ; - itemCountByIssuer(issuer: string): GraphQuery ; - itemCountByName(name: string): GraphQuery ; - itemCountCollectedBy(address: string): GraphQuery ; - itemCountSoldBy(address: string): GraphQuery ; - itemCountByCollectionId(id: string): GraphQuery ; - itemCountForSale(): GraphQuery ; - itemCountForSaleByCollectionId(id: string): GraphQuery ; - itemCountByCollectionIdAndOwner(id: string, owner: string): GraphQuery ; - itemCountCreatedAfter(date: Date): GraphQuery ; + itemCountByOwner(owner: string): GraphQuery + itemCountByIssuer(issuer: string): GraphQuery + itemCountByName(name: string): GraphQuery + itemCountCollectedBy(address: string): GraphQuery + itemCountSoldBy(address: string): GraphQuery + itemCountByCollectionId(id: string): GraphQuery + itemCountForSale(): GraphQuery + itemCountForSaleByCollectionId(id: string): GraphQuery + itemCountByCollectionIdAndOwner(id: string, owner: string): GraphQuery + itemCountCreatedAfter(date: Date): GraphQuery // itemListBy(id: string, field: KeyOf, fields?: ObjProp): GraphQuery itemListByCollectionId(id: string, options?: QueryProps): GraphQuery - itemListByCollectionIdAndOwner(id: string, owner: string, options?: QueryProps): GraphQuery + itemListByCollectionIdAndOwner( + id: string, + owner: string, + options?: QueryProps, + ): GraphQuery itemListByCollectionIdList(ids: string[], options?: QueryProps): GraphQuery itemListByIssuer(issuer: string, options?: QueryProps): GraphQuery itemListByName(name: string, options?: QueryProps): GraphQuery diff --git a/src/clients/defaults.ts b/src/clients/defaults.ts index 0064b27..e536082 100644 --- a/src/clients/defaults.ts +++ b/src/clients/defaults.ts @@ -1,30 +1,63 @@ -import { ObjProp, Fields, QueryOptions, BaseEvent, AbstractBase, QueryProps, QueryEntity } from '../types' - -export const defaultField: ObjProp = ['id', 'createdAt', 'name', 'metadata', 'currentOwner', 'issuer'] -export const defaultEventField: ObjProp = ['id', 'interaction', 'timestamp', 'caller', 'meta'] -export const DEFAULT_LIMIT = 100 +import { + AbstractBase, + BaseEvent, + Fields, + ObjProp, + QueryEntity, + QueryOptions, + QueryProps, +} from '../types' + +export const defaultField: ObjProp = [ + 'id', + 'createdAt', + 'name', + 'metadata', + 'currentOwner', + 'issuer', +] +export const defaultEventField: ObjProp = [ + 'id', + 'interaction', + 'timestamp', + 'caller', + 'meta', +] +export const DEFAULT_LIMIT = 200 // todo: add default orderBy export const defaultQueryOptions: QueryOptions = { - limit: DEFAULT_LIMIT + limit: DEFAULT_LIMIT, } function hasMetaField(field: any): boolean { return typeof field === 'string' && field === 'meta' } -export function extendFields(fields: ObjProp): ObjProp { +export function extendFields( + fields: ObjProp, +): ObjProp { const set = new Set([...defaultField, ...fields]) return [...set] } -export function getFields(fields?: ObjProp, defaultList: ObjProp | string[] = defaultField, replaceMetaField = true): Fields { +export function getFields( + fields?: ObjProp, + defaultList: ObjProp | string[] = defaultField, + replaceMetaField = true, +): Fields { const list = fields ?? defaultList if (replaceMetaField) { const metaIndex = list.findIndex(hasMetaField) if (metaIndex !== -1) { - list.splice(metaIndex, 1, { meta: ['id', 'name', 'description', 'image', 'animationUrl', 'type'] } as any) + list.splice( + metaIndex, + 1, + { + meta: ['id', 'name', 'description', 'image', 'animationUrl', 'type'], + } as any, + ) } } return list @@ -36,7 +69,7 @@ export function wrapSubqueryList(fields: Fields): [{ nodes: Fields }] { export function optionToQuery( options: QueryOptions, - injectDefault = true + injectDefault = true, ): string { const final = injectDefault ? ensureOptions(options) : options let query = '' @@ -57,7 +90,10 @@ export function ensureOptions(options?: QueryOptions): QueryOptions { return { ...defaultQueryOptions, ...queryOptions, - limit: Math.min(queryOptions.limit ?? DEFAULT_LIMIT, defaultQueryOptions.limit) + limit: Math.min( + queryOptions.limit ?? DEFAULT_LIMIT, + defaultQueryOptions.limit, + ), } } @@ -91,7 +127,7 @@ type GraphEntity = 'collection' | 'item' | 'event' export const realEntityName: Record = { collection: 'CollectionEntity', item: 'NFTEntity', - event: 'Event' + event: 'Event', } function addWhere(whereType: string, whereValue?: any) { @@ -99,15 +135,14 @@ function addWhere(whereType: string, whereValue?: any) { return { where: { type: whereType, - value: whereValue - } + value: whereValue, + }, } } return {} } - type OperationName = `${GraphEntity}Count` export function genericCountQuery(entity: GraphEntity, whereValue?: any) { const name = entity + 'Count' as OperationName @@ -120,8 +155,8 @@ export function genericCountQuery(entity: GraphEntity, whereValue?: any) { fields: ['totalCount'], variables: { orderBy: { value: 'id_ASC', type: `[${types}OrderByInput!]!` }, - ...where + ...where, }, - whereType: `${types}WhereInput` + whereType: `${types}WhereInput`, } } diff --git a/src/clients/filters.ts b/src/clients/filters.ts index 60bdb61..7df4d49 100644 --- a/src/clients/filters.ts +++ b/src/clients/filters.ts @@ -1,5 +1,9 @@ import { - FilterBuilder, FilterMappingFn, FilterOrderDirection, FilterOrderType, FilterType + FilterBuilder, + FilterMappingFn, + FilterOrderDirection, + FilterOrderType, + FilterType, } from '../types' export function getFilters(filters: FilterBuilder[]) { @@ -9,11 +13,11 @@ export function getFilters(filters: FilterBuilder[]) { function generateFilters( filters: FilterBuilder[], - mappingFn: FilterMappingFn + mappingFn: FilterMappingFn, ): string[] { return filters .flatMap(([type, directions]) => - getFilterOrders(directions).map(direction => mappingFn(type, direction)) + getFilterOrders(directions).map((direction) => mappingFn(type, direction)) ) } @@ -28,20 +32,20 @@ function generateFilters( function subsquidFilterMapping( filter: FilterType, - direction: FilterOrderDirection + direction: FilterOrderDirection, ): string { return appendFilterDirection(filter, direction) } function appendFilterDirection( filter: string, - direction: FilterOrderDirection + direction: FilterOrderDirection, ) { return filter + '_' + direction } function getFilterOrders( - filterOrders: FilterOrderType + filterOrders: FilterOrderType, ): FilterOrderDirection[] { if (!filterOrders) { return ['ASC', 'DESC'] diff --git a/src/index.ts b/src/index.ts index 295ce12..6780232 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,4 +7,4 @@ export { fetchQuery, graphFetch } from './indexers' export * from './rest' // eslint-disable-next-line unicorn/prefer-export-from -export { SquidClient, getClient, getUrl } +export { getClient, getUrl, SquidClient } diff --git a/src/indexers/index.ts b/src/indexers/index.ts index b1256b5..704cce3 100644 --- a/src/indexers/index.ts +++ b/src/indexers/index.ts @@ -8,9 +8,11 @@ function getUrl(chain: Prefix, provider: Or = ''): string { if (!result || provider === 'subquery') { const message = chain - ? `Indexer for chain ${chain} not found, allowed values are ${Object.keys( - INDEXERS - )}` + ? `Indexer for chain ${chain} not found, allowed values are ${ + Object.keys( + INDEXERS, + ) + }` : `Cannot use fetch for non-existinng chain, add parameter prefix when calling getClient or fetchQuery` throw new ReferenceError(message) } @@ -18,12 +20,18 @@ function getUrl(chain: Prefix, provider: Or = ''): string { return result } -export function graphFetch(baseURL: string, query: GraphQuery): Promise> { +export function graphFetch( + baseURL: string, + query: GraphQuery, +): Promise> { const opts = getOptions({ query, baseURL, path: '' }) return $fetch>(baseURL, opts) } -export function fetchQuery(prefix: Prefix, query: GraphQuery): Promise> { +export function fetchQuery( + prefix: Prefix, + query: GraphQuery, +): Promise> { const baseURL = getUrl(prefix) return graphFetch(baseURL, query) } diff --git a/src/indexers/utils.ts b/src/indexers/utils.ts index 1a7281d..0a24e19 100644 --- a/src/indexers/utils.ts +++ b/src/indexers/utils.ts @@ -3,11 +3,11 @@ import { GraphLike, GraphRequest } from '../rest/types' export const getOptions = ({ baseURL, - query + query, }: GraphRequest): FetchOptions<'json'> => ({ baseURL, method: 'POST', - body: query + body: query, }) export const uwrapRequest = (req: GraphLike): T => { diff --git a/src/queryBuilder.ts b/src/queryBuilder.ts index 35f87d4..1cbeb42 100644 --- a/src/queryBuilder.ts +++ b/src/queryBuilder.ts @@ -1,11 +1,15 @@ import { query } from 'gql-query-builder' -import { KeyValue, FieldList, GraphQuery } from './types' +import { FieldList, GraphQuery, KeyValue } from './types' -function build(operation: string, fields: FieldList, variables?: KeyValue): GraphQuery { +function build( + operation: string, + fields: FieldList, + variables?: KeyValue, +): GraphQuery { return query({ operation, variables, - fields + fields, }) } diff --git a/src/rest/ask.ts b/src/rest/ask.ts index d00e73e..ad5406e 100644 --- a/src/rest/ask.ts +++ b/src/rest/ask.ts @@ -9,7 +9,10 @@ import { GraphLike } from './types' const GRAPHQL_PATH = '/graphql' -function askFor(path: string, options?: QueryProps): Promise> { +function askFor( + path: string, + options?: QueryProps, +): Promise> { const request = pathToRequest(path, options) const opts = getOptions(request) return $fetch>(GRAPHQL_PATH, opts) diff --git a/src/rest/indexers.ts b/src/rest/indexers.ts index e2a1812..77f7797 100644 --- a/src/rest/indexers.ts +++ b/src/rest/indexers.ts @@ -3,4 +3,5 @@ import { Prefix } from './types' export const getUrl = (chain: Prefix | ''): string => INDEXERS[chain] -export const getAvailableChains = (): Prefix[] => Object.keys(INDEXERS) as Prefix[] +export const getAvailableChains = (): Prefix[] => + Object.keys(INDEXERS) as Prefix[] diff --git a/src/rest/path.ts b/src/rest/path.ts index 44f761b..02779b3 100644 --- a/src/rest/path.ts +++ b/src/rest/path.ts @@ -22,7 +22,7 @@ const pathMap: Record = { itemByOwner: 'itemListByOwner', itemCollectedBy: 'itemListCollectedBy', itemForSaleByCollection: 'itemListForSaleByCollectionId', - itemSoldBy: 'itemListSoldBy' + itemSoldBy: 'itemListSoldBy', } export const parsePath = (pathname: string): string[] => { @@ -37,7 +37,11 @@ const supportChain = (chain: string | undefined): boolean => { } const urlOf = (path: string): $URL => new $URL(path) -const makeQuery = (call: string, id: string, options?: QueryProps): GraphQuery => { +const makeQuery = ( + call: string, + id: string, + options?: QueryProps, +): GraphQuery => { const client = getClient() const method = getCall(call) const fn: any | undefined = client[method] @@ -50,7 +54,10 @@ const makeQuery = (call: string, id: string, options?: QueryProps): GraphQu // /bsx/item/:id // TODO: should return GraphRequest -export function pathToRequest(path: string, options?: QueryProps): GraphRequest { +export function pathToRequest( + path: string, + options?: QueryProps, +): GraphRequest { const { pathname } = urlOf(path) // query: options const [chain, call, id] = parsePath(pathname) if (!hasCall(call) || !supportChain(chain)) { diff --git a/src/rest/types.ts b/src/rest/types.ts index 0f422aa..70a8565 100644 --- a/src/rest/types.ts +++ b/src/rest/types.ts @@ -4,7 +4,7 @@ import { GraphQuery, Or } from '../types' export { GraphLike } from '../types' export type GraphRequest = { - baseURL: string, + baseURL: string query: GraphQuery path: string } diff --git a/src/types.ts b/src/types.ts index 6f47d1c..9216b43 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,17 +1,23 @@ export type KeyOf = keyof T export type ObjProp = Array> // FieldList is a type that can is array containing either a string or an object that contains a string array -export type FieldList = Array +export type FieldList = Array export type Fields = FieldList | ObjProp export type KeyValue = { - [k: string]: any; + [k: string]: any } type GraphType = any export type StrictKeyValue = { - [k: string]: any | { value: any, required?: boolean, type?: GraphType, list?: boolean, name?: string } + [k: string]: any | { + value: any + required?: boolean + type?: GraphType + list?: boolean + name?: string + } } export type GraphQuery = { @@ -105,8 +111,20 @@ export type Or = A | B // } export type FilterType = 'blockNumber' | 'updatedAt' | 'price' | 'sn' -export type QueryEntity = 'collection' | 'collections' | 'event' | 'events' | 'item' | 'items' | 'collectionCount' | 'itemCount' | 'eventCount' +export type QueryEntity = + | 'collection' + | 'collections' + | 'event' + | 'events' + | 'item' + | 'items' + | 'collectionCount' + | 'itemCount' + | 'eventCount' export type FilterOrderDirection = 'ASC' | 'DESC' export type FilterOrderType = [FilterOrderDirection, FilterOrderDirection?] export type FilterBuilder = [FilterType, FilterOrderType?] -export type FilterMappingFn = (filter: FilterType, direction: FilterOrderDirection) => string +export type FilterMappingFn = ( + filter: FilterType, + direction: FilterOrderDirection, +) => string