diff --git a/libs/adapters/src/trpc/index.ts b/libs/adapters/src/trpc/index.ts index f8b3d74385b..332606d81e0 100644 --- a/libs/adapters/src/trpc/index.ts +++ b/libs/adapters/src/trpc/index.ts @@ -209,7 +209,7 @@ export const query = ( return trpc.procedure .meta({ openapi: { - method: 'POST', + method: 'GET', path: `/${Tag.Query.toLowerCase()}/${factory.name}`, tags: [Tag.Query], headers: [{ name: 'address_id' }], diff --git a/libs/model/src/community/GetCommunities.query.ts b/libs/model/src/community/GetCommunities.query.ts index e3f8e6c7349..9244a6a20e3 100644 --- a/libs/model/src/community/GetCommunities.query.ts +++ b/libs/model/src/community/GetCommunities.query.ts @@ -12,7 +12,6 @@ export function GetCommunities(): Query { body: async ({ payload }) => { const { base, - tag_ids, include_node_info, stake_enabled, has_groups, @@ -21,6 +20,11 @@ export function GetCommunities(): Query { 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'; diff --git a/libs/model/test/community/tags.spec.ts b/libs/model/test/community/tags.spec.ts index 521f82ec1ef..a82101079f8 100644 --- a/libs/model/test/community/tags.spec.ts +++ b/libs/model/test/community/tags.spec.ts @@ -56,7 +56,7 @@ 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); }); @@ -64,8 +64,20 @@ describe('Tags', () => { 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; + } + }); }); diff --git a/libs/schemas/src/queries/community.schemas.ts b/libs/schemas/src/queries/community.schemas.ts index 9dcba656a53..beb345e34af 100644 --- a/libs/schemas/src/queries/community.schemas.ts +++ b/libs/schemas/src/queries/community.schemas.ts @@ -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(),