generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: backpack v2 endpoints for explorer (#141)
- Loading branch information
1 parent
a31e628
commit 545a650
Showing
41 changed files
with
3,360 additions
and
835 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"files.exclude": { | ||
"data*": true, | ||
// "dist": true | ||
}, | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"editor.formatOnSave": true, | ||
"cSpell.words": [ | ||
"Ethereum" | ||
], | ||
"editor.rulers": [ | ||
120 | ||
], | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true, | ||
"source.organizeImports": true | ||
}, | ||
"eslint.validate": [ | ||
"typescript" | ||
], | ||
"files.insertFinalNewline": true, | ||
"files.trimTrailingWhitespace": true, | ||
"files.trimFinalNewlines": true, | ||
"prettier.singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,60 @@ | ||
import { EmoteDefinition } from '@dcl/schemas' | ||
import { FetcherError } from '../../adapters/elements-fetcher' | ||
import { fetchAndPaginate, paginationObject } from '../../logic/pagination' | ||
import { ErrorResponse, HandlerContextWithPath, Item, PaginatedResponse } from '../../types' | ||
import { createSorting } from '../../logic/sorting' | ||
import { | ||
ErrorResponse, | ||
HandlerContextWithPath, | ||
OnChainEmote, | ||
OnChainEmoteResponse, | ||
PaginatedResponse | ||
} from '../../types' | ||
import { createFilters } from './items-commons' | ||
|
||
// TODO: change this name | ||
type ItemResponse = Item & { | ||
definition?: EmoteDefinition | ||
function mapItemToItemResponse(item: OnChainEmote, definition: EmoteDefinition | undefined): OnChainEmoteResponse { | ||
return { | ||
urn: item.urn, | ||
amount: item.individualData.length, | ||
individualData: item.individualData, | ||
name: item.name, | ||
category: item.category, | ||
rarity: item.rarity, | ||
definition | ||
} | ||
} | ||
|
||
export async function emotesHandler( | ||
context: HandlerContextWithPath<'logs' | 'emotesFetcher' | 'emoteDefinitionsFetcher', '/users/:address/emotes'> | ||
): Promise<PaginatedResponse<ItemResponse> | ErrorResponse> { | ||
const { logs, emoteDefinitionsFetcher, emotesFetcher } = context.components | ||
context: HandlerContextWithPath<'emotesFetcher' | 'emoteDefinitionsFetcher', '/users/:address/emotes'> | ||
): Promise<PaginatedResponse<OnChainEmoteResponse> | ErrorResponse> { | ||
const { emoteDefinitionsFetcher, emotesFetcher } = context.components | ||
const { address } = context.params | ||
const logger = logs.getLogger('emotes-handler') | ||
const includeDefinitions = context.url.searchParams.has('includeDefinitions') | ||
const pagination = paginationObject(context.url) | ||
const pagination = paginationObject(context.url, Number.MAX_VALUE) | ||
const filter = createFilters(context.url) | ||
const sorting = createSorting(context.url) | ||
|
||
try { | ||
const page = await fetchAndPaginate<ItemResponse>(address, emotesFetcher.fetchOwnedElements, pagination) | ||
const page = await fetchAndPaginate<OnChainEmote>( | ||
address, | ||
emotesFetcher.fetchOwnedElements, | ||
pagination, | ||
filter, | ||
sorting | ||
) | ||
|
||
if (includeDefinitions) { | ||
const emotes = page.elements | ||
const definitions = await emoteDefinitionsFetcher.fetchItemsDefinitions(emotes.map((emote) => emote.urn)) | ||
const results: ItemResponse[] = [] | ||
for (let i = 0; i < emotes.length; ++i) { | ||
results.push({ | ||
...emotes[i], | ||
definition: includeDefinitions ? definitions[i] : undefined | ||
}) | ||
} | ||
page.elements = results | ||
} | ||
const results: OnChainEmoteResponse[] = [] | ||
const emotes = page.elements | ||
const definitions = includeDefinitions | ||
? await emoteDefinitionsFetcher.fetchItemsDefinitions(emotes.map((emote) => emote.urn)) | ||
: [] | ||
|
||
return { | ||
status: 200, | ||
body: { | ||
...page | ||
} | ||
} | ||
} catch (err: any) { | ||
if (err instanceof FetcherError) { | ||
return { | ||
status: 502, | ||
body: { | ||
error: 'Cannot fetch emotes right now' | ||
} | ||
} | ||
} | ||
logger.error(err) | ||
return { | ||
status: 500, | ||
body: { | ||
error: 'Internal Server Error' | ||
} | ||
for (let i = 0; i < emotes.length; ++i) { | ||
results.push(mapItemToItemResponse(emotes[i], includeDefinitions ? definitions[i] : undefined)) | ||
} | ||
|
||
return { | ||
status: 200, | ||
body: { | ||
...page, | ||
elements: results | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { IHttpServerComponent } from '@well-known-components/interfaces' | ||
import { InvalidRequestError } from '../../types' | ||
import { FetcherError } from '../../adapters/elements-fetcher' | ||
|
||
export async function errorHandler( | ||
ctx: IHttpServerComponent.DefaultContext<object>, | ||
next: () => Promise<IHttpServerComponent.IResponse> | ||
): Promise<IHttpServerComponent.IResponse> { | ||
try { | ||
return await next() | ||
} catch (error: any) { | ||
if (error instanceof InvalidRequestError) { | ||
return Promise.resolve({ | ||
status: 400, | ||
body: { | ||
error: 'Bad request', | ||
message: error.message | ||
} | ||
}) | ||
} | ||
|
||
if (error instanceof FetcherError) { | ||
return { | ||
status: 502, | ||
body: { | ||
error: 'Internal Server Error', | ||
message: error.message | ||
} | ||
} | ||
} | ||
|
||
console.log(`Error handling ${ctx.url.toString()}: ${error.message}`) | ||
return { | ||
status: 500, | ||
body: { | ||
error: 'Internal Server Error' | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.