Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(embed): create the endpoint to generate oembed metadata #52

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(embed): create the endpoint to generate oembed metadata
  • Loading branch information
tericcabrel committed Oct 7, 2022
commit f5dc5c6ec7ce6eb0c6f0f10a06269c960d7d7a60
2 changes: 1 addition & 1 deletion .github/workflows/publish-embed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- name: Publish to NPM
run: |
yarn build
yarn build:cdn
cp package.publish.json build/package.json
cd build
yarn publish
Expand Down
1 change: 1 addition & 0 deletions apps/core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ WEB_AUTH_ERROR_URL=http:https://localhost:7500/auth/error
SESSION_LIFETIME=90# 90 days
SENTRY_DSN=
SENTRY_ENABLED=false
SNIPPET_RENDERER_URL=http:https://localhost:3000/dev
1 change: 1 addition & 0 deletions apps/core/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type EnvironmentVariables = {
REQUEST_TIMEOUT: string;
SENTRY_DSN: string;
SESSION_LIFETIME: string;
SNIPPET_RENDERER_URL: string;
WEB_APP_URL: string;
WEB_AUTH_ERROR_URL: string;
WEB_AUTH_SUCCESS_URL: string;
Expand Down
1 change: 1 addition & 0 deletions apps/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@graphql-tools/schema": "^8.5.0",
"@sharingan/database": "*",
"@sharingan/domain": "*",
"@sharingan/embed": "*",
"@sharingan/logger": "*",
"@sharingan/utils": "*",
"apollo-server-core": "^3.9.0",
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/configs/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const env: AppEnvironmentVariables = {
SENTRY_DSN: getEnv('SENTRY_DSN'),
SENTRY_ENABLED: getEnv('SENTRY_ENABLED') === 'true',
SESSION_LIFETIME: parseInt(getEnv('SESSION_LIFETIME'), 10),
SNIPPET_RENDERER_URL: getEnv('SNIPPET_RENDERER_URL'),
VALUE: getEnv('NODE_ENV'),
WEB_APP_URL: getEnv('WEB_APP_URL'),
WEB_AUTH_ERROR_URL: getEnv('WEB_AUTH_ERROR_URL'),
Expand Down
29 changes: 29 additions & 0 deletions apps/core/src/resources/snippets/handlers/generate-oembed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { snippetService } from '@sharingan/domain';
import { generateOembedMetadata } from '@sharingan/embed';
import { Request, Response } from 'express';

import { env } from '../../../configs/env';
import { logger } from '../../../configs/logger';

export const generateOembed = async (req: Request, res: Response) => {
const { id } = req.params;

try {
const snippet = await snippetService.findById(id);

const embedMetadata = generateOembedMetadata({
snippet: {
id: snippet.id,
name: snippet.name,
},
snippetRendererURL: env.SNIPPET_RENDERER_URL,
webAppURL: env.WEB_APP_URL,
});

return res.json(embedMetadata);
} catch (e) {
logger.error(e);

return res.status(404).json({ error: 'Not found' });
}
};
13 changes: 13 additions & 0 deletions apps/core/src/resources/snippets/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Router } from 'express';

import { generateOembed } from './handlers/generate-oembed';

const snippetRoute = (): Router => {
const router = Router();

router.get('/oembed/:id', generateOembed);

return router;
};

export { snippetRoute };
2 changes: 2 additions & 0 deletions apps/core/src/server/rest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Application, Request, Response } from 'express';

import { authenticationRoute } from '../resources/authentication/routes';
import { snippetRoute } from '../resources/snippets/routes';
import { errorHandlerMiddleware } from './middleware/error-middleware';
import { notFoundMiddleware } from './middleware/not-found-middleware';

Expand All @@ -15,6 +16,7 @@ export const setupRestEndpoints = (app: Application) => {

app.use('/', router);
app.use('/', authenticationRoute());
app.use('/', snippetRoute());

app.get('/', (_req: Request, res: Response) => {
res.json({ message: 'Hello from Sharingan!' });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"eslint-plugin-typescript-sort-keys": "^2.1.0",
"husky": "^8.0.1",
"prettier": "^2.7.1",
"turbo": "^1.3.1"
"turbo": "^1.5.4"
},
"packageManager": "[email protected]"
}
12 changes: 10 additions & 2 deletions packages/embed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ We use [tsup](https://github.com/egoist/tsup) under the hood to make this possib

To start the watcher, run the command:
```shell
yarn build:watch
yarn build:cdn:watch
```

## Preview a snippet
Expand All @@ -72,7 +72,7 @@ Navigate to the URL `http:https://localhost:7503` to see the result.
This part is handled inside the CI/CD, so it will not be useful to do it locally:
```shell
# Generate the assets optimized for production
yarn build
yarn build:cdn
cp package.publish.json build/package.json
cd build
# Authenticate to NPM
Expand All @@ -82,3 +82,11 @@ npm publish --access=public
```

Before publishing, make sure to upgrade the package version in the file [package.publish.json](./package.publish.json) . Check out the [Semantic versioning](https://docs.npmjs.com/about-semantic-versioning) to see how to define the version number.

## Build TS library
This package also expose functions used in others packages or apps. Run the command below to build them
```shell
yarn build:lib
```


1 change: 1 addition & 0 deletions packages/embed/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { generateOembedMetadata } from './src/oembed/index';
export { renderSnippetToHtml } from './src/renderer/index';
9 changes: 6 additions & 3 deletions packages/embed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"author": "Eric Cabrel TIOGO <[email protected]>",
"license": "MIT",
"scripts": {
"build": "tsup",
"build:watch": "tsup --watch",
"build:cdn": "tsup",
"build:cdn:watch": "tsup --watch",
"build:lib": "tsc",
"build": "yarn build:lib && yarn build:cdn",
"clean": "rm -rf .turbo dist build",
"dev": "nodemon --watch \"*.ts\" --exec \"ts-node\" ./src/server/index.ts",
"lint": "eslint src",
Expand All @@ -23,6 +25,7 @@
"serve": "^14.0.1",
"shiki": "^0.11.1",
"ts-node": "^10.9.1",
"tsup": "^6.2.2"
"tsup": "^6.2.2",
"typescript": "^4.8.4"
}
}
40 changes: 40 additions & 0 deletions packages/embed/src/oembed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
type Args = {
snippet: {
id: string;
name: string;
};
snippetRendererURL: string;
webAppURL: string;
};

type Result = {
height: number;
html: string;
provider_name: string;
provider_url: string;
thumbnail_height: string;
thumbnail_url: string;
thumbnail_width: string;
title: string;
type: string;
version: string;
width: number;
};

export const generateOembedMetadata = ({ snippet, snippetRendererURL, webAppURL }: Args): Result => {
const embedUrl = `${snippetRendererURL}/snippets/${snippet.id}`;

return {
height: 500,
html: `<iframe width="750" height="500" src="${embedUrl}" style="width:750px; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`,
provider_name: 'Sharingan',
provider_url: webAppURL,
thumbnail_height: '720',
thumbnail_url: `${webAppURL}/assets/og.png`,
thumbnail_width: '1280',
title: snippet.name,
type: 'rich',
version: '1.0',
width: 750,
};
};
133 changes: 45 additions & 88 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12422,95 +12422,47 @@ tsutils@^3.21.0:
dependencies:
tslib "^1.8.1"

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-android-arm64/-/turbo-android-arm64-1.3.1.tgz#dae9bd087d6a17409fbd10ed82489a1ff39cbcc8"
integrity sha512-JcnZh9tLbZDpKaXaao/s/k4qXt3TbNEc1xEYYXurVWnqiMueGeS7QAtThVB85ZSqzj7djk+ngSrZabPy5RG25Q==

[email protected]:
version "1.3.1"
resolved "https://registry.npmjs.org/turbo-darwin-64/-/turbo-darwin-64-1.3.1.tgz"
integrity sha512-TIGDradVFoGck86VIuM38KaDeNxdKaP2ti93UpQeFw26ZhPIeTAa6wUgnz4DQP6bjIvQmXlYJ16ETZb4tFYygg==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-darwin-arm64/-/turbo-darwin-arm64-1.3.1.tgz#94a361e4e73ad02995303cf6c3d8cb03e85fcae4"
integrity sha512-aLBq8KiMMmop7uKBkvDt/y+eER2UzxZyUzh1KWcZ7DZB5tFZnknEUyf2qggY2vd2WcDVfQ1EUjZ0MFxhhVaVzA==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-freebsd-64/-/turbo-freebsd-64-1.3.1.tgz#64b8fc1b9f2ad9b76b532ffdcce267b2934ecb86"
integrity sha512-BOr/ifmxjlBeuDkDQLUJtzqzXQ2zPHHcI14U9Ys+z4Mza1uzQn/oSJqQvU5RuyRBVai7noMrpPS7QuKtDz0Cyg==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-freebsd-arm64/-/turbo-freebsd-arm64-1.3.1.tgz#b798af9b8898210058ca2752eadb711369f9ecea"
integrity sha512-bHPZjK4xnGLz6/oxl5XmWhdYOdtBMSadrGhptWSZ0wBGNn/gQzDTeZAkQeqhh25AD0eM1hzDe8QUz8GlS43lrA==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-linux-32/-/turbo-linux-32-1.3.1.tgz#9eecada1c13f6391a7c6267349a0486987144093"
integrity sha512-c5okimusfvivu9wS8MKSr+rXpQAV+M4TyR9JX+spIK8B1I7AjfECAqiK2D5WFWO1bQ33bUAuxXOEpUuLpgEm+g==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-linux-64/-/turbo-linux-64-1.3.1.tgz#eaf195b90a80f238561ab11fbde17c07af481c57"
integrity sha512-O0pNX+N5gbmRcyZT+jsCPUNCN3DpIZHqNN35j7MT5nr0IkZa83CGbZnrEc+7Qws//jFJ26EngqD/JyRB2E8nwQ==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-linux-arm64/-/turbo-linux-arm64-1.3.1.tgz#ff9dc17c352c5f59440cb55cff59cb8b23db9d1b"
integrity sha512-D6+1MeS/x+/VCCooHPU4NIpB8qI/eW70eMRA79bqTPaxxluP0g2CaxXgucco05P51YtNsSxeVcH7X76iadON6Q==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-linux-arm/-/turbo-linux-arm-1.3.1.tgz#6be53e62a60a3c6f3ab33585f0442eb7441ecf3a"
integrity sha512-f+r6JIwv/7ylxxJtgVi8cVw+6oNoD/r1IMTU6ejH8bfyMZZko4kkNwH9VYribQ44KDkJEgzdltnzFG5f6Hz10g==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-linux-mips64le/-/turbo-linux-mips64le-1.3.1.tgz#ceddf8c8e222e66d05f7a68a71f66a2a3a0272a3"
integrity sha512-yL64jgwVCziOpBcdpMxIsczkgwwOvmaqKObFKWyCNlk/LOl5NKODLwXEaryLaALtpwUAoS4ltMSI64gKqmLrOA==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-linux-ppc64le/-/turbo-linux-ppc64le-1.3.1.tgz#aa5658f6d19775e06b9fd136491dceab93ecffb2"
integrity sha512-tjnM+8RosykS1lBpOPLDXGOz/Po2h796ty17uBd7IFslWPOI16a/akFOFoLH8PCiGGJMe3CYgRhEKn4sPWNxFA==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-windows-32/-/turbo-windows-32-1.3.1.tgz#e6e570bb381d9a4f78ea6e72102ab9bf493a3ad2"
integrity sha512-Snnv+TVigulqwK6guHKndMlrLw88NXj8BtHRGrEksPR0QkyuHlwLf+tHYB4HmvpUl4W9lnXQf4hsljWP64BEdw==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-windows-64/-/turbo-windows-64-1.3.1.tgz#f891097331125c935cdaa160cc80c4cfc3e61b0e"
integrity sha512-gLeohHG07yIhON1Pp0YNE00i/yzip2GFhkA6HdJaK95uE5bKULpqxuO414hOS/WzGwrGVXBKCImfe24XXh5T+Q==

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/turbo-windows-arm64/-/turbo-windows-arm64-1.3.1.tgz#5abe1743b93e272641018cef213b21ac6c984f04"
integrity sha512-0MWcHLvYgs/qdcoTFZ55nu8HhrpeiwXEMw9cbNfgqTlzy3OsrAsovYEJFyQ8KSxeploiD+QJlCdvhxx+5C0tlA==

turbo@^1.3.1:
version "1.3.1"
resolved "https://registry.npmjs.org/turbo/-/turbo-1.3.1.tgz"
integrity sha512-DXckoGKlZgvTn/PrHpBI/57aeXR7tfyPf2dK+4LmBczt24ELA3o6eYHeA7KzfpSYhB2LE9qveYFQ6mJ1OzGjjg==
[email protected]:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo-darwin-64/-/turbo-darwin-64-1.5.5.tgz#710d4e7999066bd4f500456f7cd1c30f6e6205ed"
integrity sha512-HvEn6P2B+NXDekq9LRpRgUjcT9/oygLTcK47U0qsAJZXRBSq/2hvD7lx4nAwgY/4W3rhYJeWtHTzbhoN6BXqGQ==

[email protected]:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo-darwin-arm64/-/turbo-darwin-arm64-1.5.5.tgz#f7955a800e945ab110e8a6e23f60a2b9795296ab"
integrity sha512-Dmxr09IUy6M0nc7/xWod9galIO2DD500B75sJSkHeT+CCdJOWnlinux0ZPF8CSygNqymwYO8AO2l15/6yxcycg==

[email protected]:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo-linux-64/-/turbo-linux-64-1.5.5.tgz#f31eb117a9b605f5731048c50473bff903850047"
integrity sha512-wd07TZ4zXXWjzZE00FcFMLmkybQQK/NV9ff66vvAV0vdiuacSMBCNLrD6Mm4ncfrUPW/rwFW5kU/7hyuEqqtDw==

[email protected]:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo-linux-arm64/-/turbo-linux-arm64-1.5.5.tgz#b9ce6912ae6477e829355d6f012500bfef58669d"
integrity sha512-q3q33tuo74R7gicnfvFbnZZvqmlq7Vakcvx0eshifnJw4PR+oMnTCb4w8ElVFx070zsb8DVTibq99y8NJH8T1Q==

[email protected]:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo-windows-64/-/turbo-windows-64-1.5.5.tgz#609098de3bc6178f733615d21b06d5c1602637eb"
integrity sha512-lPp9kHonNFfqgovbaW+UAPO5cLmoAN+m3G3FzqcrRPnlzt97vXYsDhDd/4Zy3oAKoAcprtP4CGy0ddisqsKTVw==

[email protected]:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo-windows-arm64/-/turbo-windows-arm64-1.5.5.tgz#60522e1e347a54c64bdddb68089fc322ee19c3d7"
integrity sha512-3AfGULKNZiZVrEzsIE+W79ZRW1+f5r4nM4wLlJ1PTBHyRxBZdD6KTH1tijGfy/uTlcV5acYnKHEkDc6Q9PAXGQ==

turbo@^1.5.4:
version "1.5.5"
resolved "https://registry.yarnpkg.com/turbo/-/turbo-1.5.5.tgz#9fc3a917c914ffa113c260a4eadb4bc632eee227"
integrity sha512-PVQSDl0STC9WXIyHcYUWs9gXsf8JjQig/FuHfuB8N6+XlgCGB3mPbfMEE6zrChGz2hufH4/guKRX1XJuNL6XTA==
optionalDependencies:
turbo-android-arm64 "1.3.1"
turbo-darwin-64 "1.3.1"
turbo-darwin-arm64 "1.3.1"
turbo-freebsd-64 "1.3.1"
turbo-freebsd-arm64 "1.3.1"
turbo-linux-32 "1.3.1"
turbo-linux-64 "1.3.1"
turbo-linux-arm "1.3.1"
turbo-linux-arm64 "1.3.1"
turbo-linux-mips64le "1.3.1"
turbo-linux-ppc64le "1.3.1"
turbo-windows-32 "1.3.1"
turbo-windows-64 "1.3.1"
turbo-windows-arm64 "1.3.1"
turbo-darwin-64 "1.5.5"
turbo-darwin-arm64 "1.5.5"
turbo-linux-64 "1.5.5"
turbo-linux-arm64 "1.5.5"
turbo-windows-64 "1.5.5"
turbo-windows-arm64 "1.5.5"

type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0"
Expand Down Expand Up @@ -12596,6 +12548,11 @@ typescript@^4.8.2:
resolved "https://registry.npmjs.org/typescript/-/typescript-4.8.2.tgz"
integrity sha512-C0I1UsrrDHo2fYI5oaCGbSejwX4ch+9Y5jTQELvovfmFkK3HHSZJB8MSJcWLmCUBzQBchCrZ9rMRV6GuNrvGtw==

typescript@^4.8.4:
version "4.8.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==

ua-parser-js@^0.7.30:
version "0.7.31"
resolved "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.31.tgz"
Expand Down