Skip to content

Commit

Permalink
Switch TRPC back to GET and use preprocess for tag_ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnaviask committed Aug 7, 2024
1 parent 7dd9196 commit a69aa60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libs/adapters/src/trpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export const query = <Input extends ZodSchema, Output extends ZodSchema>(
return trpc.procedure
.meta({
openapi: {
method: 'POST',
method: 'GET',
path: `/${Tag.Query.toLowerCase()}/${factory.name}`,
tags: [Tag.Query],
headers: [{ name: 'address_id' }],
Expand Down
6 changes: 5 additions & 1 deletion libs/model/src/community/GetCommunities.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function GetCommunities(): Query<typeof schemas.GetCommunities> {
body: async ({ payload }) => {
const {
base,
tag_ids,
include_node_info,
stake_enabled,
has_groups,
Expand All @@ -21,6 +20,11 @@ export function GetCommunities(): Query<typeof schemas.GetCommunities> {
order_by,
order_direction,
} = payload;
// NOTE 8/7/24: as a result of having to provide a preprocessed string instead of
// being able to provide an array, we must explicitly set tag_ids as type number[]
// here. The input parser will handle incorrectly formatted strings (see the
// tags.spec.ts file where it is exercised) so this is safe to do.
const tag_ids = payload.tag_ids as number[];

// pagination configuration
const direction = order_direction || 'DESC';
Expand Down
16 changes: 14 additions & 2 deletions libs/model/test/community/tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,28 @@ describe('Tags', () => {
test('should query both tagged communities with tag 1 provided', async () => {
const communityResults = await query(GetCommunities(), {
actor,
payload: { tag_ids: [tag1Id] },
payload: { tag_ids: [tag1Id].join(',') },
});
expect(communityResults?.results).to.have.length(2);
});

test('should query single community with tag 1 and 2 provided', async () => {
const communityResults = await query(GetCommunities(), {
actor,
payload: { tag_ids: [tag1Id, tag2Id] },
payload: { tag_ids: [tag1Id, tag2Id].join(',') },
});
expect(communityResults?.results).to.have.length(1);
});

test('should fail on invalid tag string', async () => {
try {
await query(GetCommunities(), {
actor,
payload: { tag_ids: 'abkjdgkjsagj,daskgjdsakgjsdg' },
});
} catch (e) {
expect((e as Error).message).to.include('Invalid query payload');
return;
}
});
});
15 changes: 10 additions & 5 deletions libs/schemas/src/queries/community.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import { PaginatedResultSchema, PaginationParamsSchema } from './pagination';
export const GetCommunities = {
input: PaginationParamsSchema.extend({
base: z.nativeEnum(ChainBase).optional(),
// NOTE 8/7/24: passing arrays as GET requests is not supported
// to support this field, we converted queries to use POST instead
// but we may need a workaround such as a regex-parsed string
// in the future, depending on the impact of the GET-POST change.
tag_ids: PG_INT.array().optional(),
// NOTE 8/7/24: passing arrays in GET requests directly is not supported.
// Instead we support comma-separated strings of ids.
tag_ids: z
.preprocess((value) => {
if (typeof value === 'string') {
return value.split(',').map((id) => id.trim());
}
return value;
}, z.array(z.coerce.number().positive()))
.optional(),
include_node_info: z.boolean().optional(),
stake_enabled: z.boolean().optional(),
has_groups: z.boolean().optional(),
Expand Down

0 comments on commit a69aa60

Please sign in to comment.