Commit 6b6b965e authored by tom's avatar tom

search result redirects

parent 77e0febf
import type { IncomingMessage } from 'http';
import type { NextApiRequest } from 'next';
import type { NextApiRequestCookies } from 'next/dist/server/api-utils';
import type { RequestInit, Response } from 'node-fetch';
import nodeFetch from 'node-fetch';
......@@ -6,7 +8,7 @@ import { httpLogger } from 'lib/api/logger';
import * as cookies from 'lib/cookies';
export default function fetchFactory(
_req: NextApiRequest,
_req: NextApiRequest | (IncomingMessage & { cookies: NextApiRequestCookies }),
) {
// first arg can be only a string
// FIXME migrate to RequestInfo later if needed
......
......@@ -240,6 +240,9 @@ export const RESOURCES = {
],
filterFields: [ 'q' ],
},
search_check_redirect: {
path: '/api/v2/search/check-redirect',
},
// DEPRECATED
old_api: {
......
import type { GetServerSideProps, GetServerSidePropsResult } from 'next';
import type { GetServerSideProps } from 'next';
export type Props = {
cookies: string;
......@@ -6,7 +6,7 @@ export type Props = {
id?: string;
}
export const getServerSideProps: GetServerSideProps = async({ req, query }): Promise<GetServerSidePropsResult<Props>> => {
export const getServerSideProps: GetServerSideProps<Props> = async({ req, query }) => {
return {
props: {
cookies: req.headers.cookie || '',
......
import type { NextPage } from 'next';
import type { GetServerSideProps, NextPage } from 'next';
import Head from 'next/head';
import React from 'react';
import type { SearchRedirectResult } from 'types/api/search';
import buildUrlNode from 'lib/api/buildUrlNode';
import fetchFactory from 'lib/api/nodeFetch';
import link from 'lib/link/link';
import getNetworkTitle from 'lib/networks/getNetworkTitle';
import type { Props } from 'lib/next/getServerSideProps';
import { getServerSideProps as getServerSidePropsBase } from 'lib/next/getServerSideProps';
import SearchResults from 'ui/pages/SearchResults';
const SearchResultsPage: NextPage = () => {
......@@ -19,4 +26,42 @@ const SearchResultsPage: NextPage = () => {
export default SearchResultsPage;
export { getServerSideProps } from 'lib/next/getServerSideProps';
export const getServerSideProps: GetServerSideProps<Props> = async({ req, res, resolvedUrl, query }) => {
try {
const q = String(query.q);
const url = buildUrlNode('search_check_redirect', undefined, { q });
const redirectsResponse = await fetchFactory(req)(url);
const payload = await redirectsResponse.json() as SearchRedirectResult;
if (!payload || typeof payload !== 'object' || !payload.redirect) {
throw Error();
}
const redirectUrl = (() => {
switch (payload.type) {
case 'block': {
return link('block', { id: q });
}
case 'address': {
return link('address_index', { id: payload.parameter || q });
}
case 'transaction': {
return link('tx', { id: q });
}
}
})();
if (!redirectUrl) {
throw Error();
}
return {
redirect: {
destination: redirectUrl,
permanent: false,
},
};
} catch (error) {}
return getServerSidePropsBase({ req, res, resolvedUrl, query });
};
......@@ -49,3 +49,9 @@ export interface SearchResult {
export interface SearchResultFilters {
q: string;
}
export interface SearchRedirectResult {
parameter: string | null;
redirect: boolean;
type: 'address' | 'block' | 'transaction' | null;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment