Skip to content

Commit

Permalink
fix: add /metadata/v1 prefix to all routes (#100)
Browse files Browse the repository at this point in the history
* fix: /metadata/v1 prefix

* fix: rewrite non-versioned urls

* test: fix status

* fix: move rewrites to actual prefixes

* chore: remove unused import

* test: non-versioned urls

* test: real non version responses
  • Loading branch information
rafaelcr authored Feb 7, 2023
1 parent fb3e6c6 commit a11d4be
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NftRoutes } from './routes/nft';
import { SftRoutes } from './routes/sft';
import { PgStore } from '../pg/pg-store';
import FastifyCors from '@fastify/cors';
import FastifySwagger, { SwaggerOptions } from '@fastify/swagger';
import { SwaggerOptions } from '@fastify/swagger';
import { StatusRoutes } from './routes/status';
import FastifyMetrics from 'fastify-metrics';
import { Server } from 'http';
Expand Down Expand Up @@ -57,11 +57,10 @@ export async function buildApiServer(args: { db: PgStore }) {
fastify.decorate('db', args.db);
if (process.env['NODE_ENV'] === 'production') {
await fastify.register(FastifyMetrics);
} else if (process.env['NODE_ENV'] === 'development') {
await fastify.register(FastifySwagger, ApiSwaggerOptions);
}
await fastify.register(FastifyCors);
await fastify.register(Api);
await fastify.register(Api, { prefix: '/metadata/v1' });
await fastify.register(Api, { prefix: '/metadata' });

return fastify;
}
16 changes: 11 additions & 5 deletions tests/ft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('FT routes', () => {
test('token not found', async () => {
const response = await fastify.inject({
method: 'GET',
url: '/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
url: '/metadata/v1/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
});
expect(response.statusCode).toBe(404);
expect(response.json()).toStrictEqual({ error: 'Token not found' });
Expand All @@ -49,7 +49,7 @@ describe('FT routes', () => {
await enqueueToken();
const response = await fastify.inject({
method: 'GET',
url: '/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
url: '/metadata/v1/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
});
expect(response.statusCode).toBe(422);
expect(response.json()).toStrictEqual({ error: 'Token metadata fetch in progress' });
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('FT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world?locale=es',
url: '/metadata/v1/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world?locale=es',
});
expect(response.statusCode).toBe(422);
expect(response.json()).toStrictEqual({ error: 'Locale not found' });
Expand All @@ -108,7 +108,7 @@ describe('FT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
url: '/metadata/v1/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual({
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('FT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
url: '/metadata/v1/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual({
Expand Down Expand Up @@ -204,5 +204,11 @@ describe('FT routes', () => {
},
},
});
const noVersionResponse = await fastify.inject({
method: 'GET',
url: '/metadata/ft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world',
});
expect(response.statusCode).toEqual(noVersionResponse.statusCode);
expect(response.json()).toStrictEqual(noVersionResponse.json());
});
});
16 changes: 11 additions & 5 deletions tests/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('NFT routes', () => {
test('token not found', async () => {
const response = await fastify.inject({
method: 'GET',
url: '/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
url: '/metadata/v1/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
});
expect(response.statusCode).toBe(404);
expect(response.json()).toStrictEqual({ error: 'Token not found' });
Expand All @@ -49,7 +49,7 @@ describe('NFT routes', () => {
await enqueueToken();
const response = await fastify.inject({
method: 'GET',
url: '/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
url: '/metadata/v1/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
});
expect(response.statusCode).toBe(422);
expect(response.json()).toStrictEqual({ error: 'Token metadata fetch in progress' });
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('NFT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1?locale=es',
url: '/metadata/v1/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1?locale=es',
});
expect(response.statusCode).toBe(422);
expect(response.json()).toStrictEqual({ error: 'Locale not found' });
Expand All @@ -108,7 +108,7 @@ describe('NFT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
url: '/metadata/v1/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual({ token_uri: 'http://test.com/uri.json' });
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('NFT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
url: '/metadata/v1/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual({
Expand All @@ -194,5 +194,11 @@ describe('NFT routes', () => {
},
},
});
const noVersionResponse = await fastify.inject({
method: 'GET',
url: '/metadata/nft/SP2SYHR84SDJJDK8M09HFS4KBFXPPCX9H7RZ9YVTS.hello-world/1',
});
expect(response.statusCode).toEqual(noVersionResponse.statusCode);
expect(response.json()).toStrictEqual(noVersionResponse.json());
});
});
16 changes: 11 additions & 5 deletions tests/sft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('SFT routes', () => {
test('token not found', async () => {
const response = await fastify.inject({
method: 'GET',
url: '/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
url: '/metadata/v1/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
});
expect(response.statusCode).toBe(404);
expect(response.json()).toStrictEqual({ error: 'Token not found' });
Expand All @@ -53,7 +53,7 @@ describe('SFT routes', () => {
await enqueueToken();
const response = await fastify.inject({
method: 'GET',
url: '/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
url: '/metadata/v1/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
});
expect(response.statusCode).toBe(422);
expect(response.json()).toStrictEqual({ error: 'Token metadata fetch in progress' });
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('SFT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1?locale=es',
url: '/metadata/v1/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1?locale=es',
});
expect(response.statusCode).toBe(422);
expect(response.json()).toStrictEqual({ error: 'Locale not found' });
Expand All @@ -112,7 +112,7 @@ describe('SFT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
url: '/metadata/v1/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual({
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('SFT routes', () => {
});
const response = await fastify.inject({
method: 'GET',
url: '/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
url: '/metadata/v1/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
});
expect(response.statusCode).toBe(200);
expect(response.json()).toStrictEqual({
Expand Down Expand Up @@ -204,5 +204,11 @@ describe('SFT routes', () => {
},
},
});
const noVersionResponse = await fastify.inject({
method: 'GET',
url: '/metadata/sft/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.key-alex-autoalex-v1/1',
});
expect(response.statusCode).toEqual(noVersionResponse.statusCode);
expect(response.json()).toStrictEqual(noVersionResponse.json());
});
});
7 changes: 5 additions & 2 deletions tests/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ describe('Status routes', () => {
});

test('returns status when nothing has been processed', async () => {
const response = await fastify.inject({ method: 'GET', url: '/' });
const response = await fastify.inject({ method: 'GET', url: '/metadata/v1/' });
const json = response.json();
expect(json).toStrictEqual({
server_version: 'token-metadata-service v0.0.1 (test:123456)',
status: 'ready',
});
const noVersionResponse = await fastify.inject({ method: 'GET', url: '/metadata/' });
expect(response.statusCode).toEqual(noVersionResponse.statusCode);
expect(json).toStrictEqual(noVersionResponse.json());
});

test('returns status counts', async () => {
Expand All @@ -51,7 +54,7 @@ describe('Status routes', () => {
type: DbTokenType.nft,
});

const response = await fastify.inject({ method: 'GET', url: '/' });
const response = await fastify.inject({ method: 'GET', url: '/metadata/v1/' });
const json = response.json();
expect(json).toStrictEqual({
server_version: 'token-metadata-service v0.0.1 (test:123456)',
Expand Down
2 changes: 1 addition & 1 deletion util/openapi-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const ApiGenerator: FastifyPluginAsync<
TypeBoxTypeProvider
> = async (fastify, options) => {
await fastify.register(FastifySwagger, ApiSwaggerOptions);
await fastify.register(Api);
await fastify.register(Api, { prefix: '/metadata/v1' });
if (!existsSync('./tmp')) {
mkdirSync('./tmp');
}
Expand Down

0 comments on commit a11d4be

Please sign in to comment.