Skip to content

Commit 453b9f4

Browse files
authored
fix: send full url when requesting the short link (#2025)
* fix: send full url when requesting the short link * fix: tests * fix: use a better var name and default value
1 parent c0173e7 commit 453b9f4

6 files changed

Lines changed: 35 additions & 6 deletions

File tree

.devcontainer/docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ services:
3535
S3_FILE_ITEM_HOST: http://s3.garage.localhost:3900
3636
# the Iframely config is set by the "iframely" service below
3737
EMBEDDED_LINK_ITEM_IFRAMELY_HREF_ORIGIN: http://iframely:8061
38+
# Redirection for shortlinks
39+
SHORT_LINK_BASE_URL: http://localhost:3000/short-links
3840
links:
3941
- garage:s3.garage.localhost
4042
volumes:

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ CLIENT_HOST=http://localhost:3114
207207
LIBRARY_CLIENT_HOST=http://localhost:3005
208208
GRAASP_MOBILE_BUILDER=graasp-mobile-builder
209209

210+
# Base url used to redirect shortlink aliases
211+
# SHORT_LINK_BASE_URL=http://localhost:3000/short-links
212+
210213
# This is already set in the docker-compose file, un-comment below if you want to override it
211214
# IMAGE_CLASSIFIER_API=<url>
212215

src/config/hosts.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getEnv } from './env';
2+
3+
getEnv();
4+
5+
export const SHORT_LINK_BASE_URL =
6+
process.env.SHORT_LINK_BASE_URL ?? 'http://localhost:3000/short-links';

src/services/item/plugins/shortLink/shortlink.controller.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import build, {
1111
unmockAuthenticate,
1212
} from '../../../../../test/app';
1313
import { seedFromJson } from '../../../../../test/mocks/seed';
14+
import { SHORT_LINK_BASE_URL } from '../../../../config/hosts';
1415
import { db } from '../../../../drizzle/db';
1516
import { shortLinksTable } from '../../../../drizzle/schema';
1617
import { assertIsDefined } from '../../../../utils/assertions';
@@ -765,7 +766,12 @@ describe('Short links routes tests', () => {
765766

766767
const response = await injectGetAll(app, item.id);
767768
expect(response.statusCode).toEqual(StatusCodes.OK);
768-
expect(response.json()).toEqual({ [shortLink.platform]: shortLink.alias });
769+
expect(response.json()).toEqual({
770+
[shortLink.platform]: {
771+
alias: shortLink.alias,
772+
url: `${SHORT_LINK_BASE_URL}/${shortLink.alias}`,
773+
},
774+
});
769775
});
770776
it('Success if read permission', async () => {
771777
const {

src/services/item/plugins/shortLink/shortlink.schemas.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const shortLink = customType.StrictObject({
1515
});
1616

1717
const shortLinkAlias = Type.Pick(shortLink, ['alias']);
18+
const shortLinkPlatformResponse = Type.Object({
19+
alias: Type.String(),
20+
url: Type.String({ format: 'uri' }),
21+
});
1822

1923
export const getRedirection = {
2024
operationId: 'getShortLinkRedirection',
@@ -56,9 +60,9 @@ export const getAllByItem = {
5660
[StatusCodes.OK]: Type.Union([
5761
customType.StrictObject(
5862
{
59-
[ShortLinkPlatform.Builder]: Type.Optional(Type.String()),
60-
[ShortLinkPlatform.Player]: Type.Optional(Type.String()),
61-
[ShortLinkPlatform.Library]: Type.Optional(Type.String()),
63+
[ShortLinkPlatform.Builder]: Type.Optional(shortLinkPlatformResponse),
64+
[ShortLinkPlatform.Player]: Type.Optional(shortLinkPlatformResponse),
65+
[ShortLinkPlatform.Library]: Type.Optional(shortLinkPlatformResponse),
6266
},
6367
{ minProperties: 1 },
6468
),

src/services/item/plugins/shortLink/shortlink.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type UpdateShortLink,
1111
} from '@graasp/sdk';
1212

13+
import { SHORT_LINK_BASE_URL } from '../../../../config/hosts';
1314
import { type DBConnection } from '../../../../drizzle/db';
1415
import type { AuthenticatedUser, MinimalMember } from '../../../../types';
1516
import { ITEMS_ROUTE_PREFIX } from '../../../../utils/config';
@@ -79,14 +80,21 @@ export class ShortLinkService {
7980
});
8081

8182
const res = await this.shortLinkRepository.getByItem(dbConnection, itemId);
82-
8383
return res.reduce<ShortLinksOfItem>((acc, { alias, platform }) => {
8484
if (acc[platform]) {
8585
// This should never happen.
8686
throw new Error(`An alias for the platform "${platform}" already exist!`);
8787
}
8888

89-
return { ...acc, [platform]: alias };
89+
return {
90+
...acc,
91+
[platform]: {
92+
alias,
93+
// something of the form: https://go.graasp.org/:alias
94+
// or in local it would be: http://localhost:3000/short-links/:alias
95+
url: `${SHORT_LINK_BASE_URL}/${alias}`,
96+
},
97+
};
9098
}, {});
9199
}
92100

0 commit comments

Comments
 (0)