Skip to content

Commit

Permalink
refactor(cli): polish code per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
charIeszhao committed Jul 31, 2024
1 parent 4bf4e59 commit bacd6c7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 95 deletions.
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"devDependencies": {
"@silverhand/eslint-config": "6.0.1",
"@silverhand/ts-config": "6.0.0",
"@types/http-proxy-middleware": "^1.0.0",
"@types/inquirer": "^9.0.0",
"@types/node": "^20.9.5",
"@types/semver": "^7.3.12",
Expand Down
21 changes: 0 additions & 21 deletions packages/cli/src/commands/proxy/consts.ts

This file was deleted.

60 changes: 20 additions & 40 deletions packages/cli/src/commands/proxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,56 @@ import type { CommandModule } from 'yargs';

import { consoleLog } from '../../utils.js';

import {
createOidcResponseHandler,
createProxy,
isLogtoRequestPath,
isSignInExperienceRequestPath,
} from './utils.js';
import { createOidcResponseHandler, createProxy, isLogtoRequestPath } from './utils.js';

const proxy: CommandModule<
unknown,
{
u?: string;
t?: string;
p: number;
d: boolean;
url?: string;
tenant?: string;
port: number;
endpoint?: string;
}
> = {
command: ['proxy'],
describe: 'Command for Logto proxy',
builder: (yargs) =>
yargs
.options({
u: {
alias: ['url', 'sign-in-experience-url'],
url: {
alias: ['u', 'sign-in-experience-url'],
describe: 'The URL of your custom sign-in experience page',
type: 'string',
},
t: {
alias: ['tenant', 'tenant-id'],
tenant: {
alias: ['t', 'tenant-id'],
describe: 'The ID of your Logto Cloud tenant',
type: 'string',
},
p: {
alias: 'port',
port: {
alias: 'p',
describe: 'The port number where the proxy server will be running on',
type: 'number',
default: 9000,
},
d: {
alias: 'dev',
describe: 'Enable development features',
type: 'boolean',
default: false,
endpoint: {
alias: 'logto-endpoint',
describe:
'[Internal] Specify Logto Cloud endpoint domain. E.g. `app.logto.dev` for development',
type: 'string',
hidden: true,
},
})
.global('e'),
handler: async ({ u: signInExpUrl, t: tenantId, p: port, d: isDev }) => {
handler: async ({ url: signInExpUrl, tenant: tenantId, port, endpoint }) => {
if (!signInExpUrl) {
consoleLog.fatal('No sign-in experience URL provided.');
}
if (!tenantId) {
if (!tenantId && !endpoint) {
consoleLog.fatal('No tenant ID provided.');
}

const logtoCloudDomain = isDev ? 'app.logto.dev' : 'logto.app';
const logtoCloudEndpointUrl = new URL(`https://${tenantId}.${logtoCloudDomain}`);
const logtoCloudEndpointUrl = new URL(endpoint ?? `https://${tenantId}.logto.app}`);
const proxyUrl = new URL(`http://localhost:${port}`);

const proxyOidcRequest = createProxy(
Expand All @@ -85,21 +79,7 @@ const proxy: CommandModule<
return;
}

// Proxy the request to the sign-in experience URL
if (isSignInExperienceRequestPath(request.url)) {
void proxySignInExpRequest(request, response);
return;
}

// TODO: add more rich content as user guide
if (request.url === '/') {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end('Your Logto proxy server is up and running.');
return;
}

response.writeHead(404);
response.end('Not Found');
void proxySignInExpRequest(request, response);
});

server.listen(port, () => {
Expand Down
11 changes: 0 additions & 11 deletions packages/cli/src/commands/proxy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { type OnProxyEvent } from 'http-proxy-middleware/dist/types.js';

import { consoleLog } from '../../utils.js';

import { fileAssetPathRegex, LogtoSignInExperienceRoutes } from './consts.js';
import { type ProxyResponseHandler } from './types.js';

export const createProxy = (targetUrl: string, onProxyResponse?: OnProxyEvent['proxyRes']) => {
Expand Down Expand Up @@ -66,13 +65,3 @@ export const createOidcResponseHandler = async ({
*/
export const isLogtoRequestPath = (requestPath?: string) =>
['/oidc/', '/api/'].some((path) => requestPath?.startsWith(path)) || requestPath === '/consent';

/**
* Check if the request path is a sign-in experience router path.
* @example isSignInExperienceRequestPath('/sign-in') // true
* @example isSignInExperienceRequestPath('/register') // true
* @example isSignInExperienceRequestPath('/forgot-password') // true
*/
export const isSignInExperienceRequestPath = (requestPath?: string) =>
LogtoSignInExperienceRoutes.some((path) => requestPath?.startsWith(path)) ||
Boolean(requestPath && fileAssetPathRegex.test(requestPath));
31 changes: 9 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bacd6c7

Please sign in to comment.