-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Frontend] Migrate to create-react-app (#3156)
* Updated create-react-app dependencies * Fix some type errors * Fix type errors * Fix linting and typing * Fix more things * Fix typestyle mock * Fix some unit tests * Update snapshots * Frontend server should use its own configuration * Clean up server tsconfig.json and tslint.json * Fix mock-backend * Frontend image cloudbuild config
- Loading branch information
Showing
58 changed files
with
24,844 additions
and
12,355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
extends: react-app | ||
ignorePatterns: | ||
- node_modules/ | ||
- src/apis # these are generated api clients | ||
- '*.test.ts' | ||
- '*.test.tsx' | ||
|
||
rules: | ||
react/jsx-no-target-blank: ['error', { "allowReferrer": true }] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import * as express from 'express'; | ||
import proxy from 'http-proxy-middleware'; | ||
import { URL, URLSearchParams } from 'url'; | ||
|
||
export function _extractUrlFromReferer(proxyPrefix: string, referer = ''): string { | ||
const index = referer.indexOf(proxyPrefix); | ||
return index > -1 ? referer.substr(index + proxyPrefix.length) : ''; | ||
} | ||
|
||
export function _trimProxyPrefix(proxyPrefix: string, path: string): string { | ||
return path.indexOf(proxyPrefix) === 0 ? (path = path.substr(proxyPrefix.length)) : path; | ||
} | ||
|
||
export function _routePathWithReferer(proxyPrefix: string, path: string, referer = ''): string { | ||
// If a referer header is included, extract the referer URL, otherwise | ||
// just trim out the /_proxy/ prefix. Use the origin of the resulting URL. | ||
const proxiedUrlInReferer = _extractUrlFromReferer(proxyPrefix, referer); | ||
let decodedPath = decodeURIComponent(proxiedUrlInReferer || _trimProxyPrefix(proxyPrefix, path)); | ||
if (!decodedPath.startsWith('http://') && !decodedPath.startsWith('https://')) { | ||
decodedPath = 'http://' + decodedPath; | ||
} | ||
return new URL(decodedPath).origin; | ||
} | ||
|
||
export function _rewritePath(proxyPrefix: string, path: string, query: string): string { | ||
// Trim the proxy prefix if exists. It won't exist for any requests made | ||
// to absolute paths by the proxied resource. | ||
const querystring = new URLSearchParams(query).toString(); | ||
const decodedPath = decodeURIComponent(path); | ||
return _trimProxyPrefix(proxyPrefix, decodedPath) + (querystring && '?' + querystring); | ||
} | ||
|
||
export default (app: express.Application, apisPrefix: string) => { | ||
const proxyPrefix = apisPrefix + '/_proxy/'; | ||
|
||
app.use((req, _, next) => { | ||
// For any request that has a proxy referer header but no proxy prefix, | ||
// prepend the proxy prefix to it and redirect. | ||
if (req.headers.referer) { | ||
const refererUrl = _extractUrlFromReferer(proxyPrefix, req.headers.referer as string); | ||
if (refererUrl && req.url.indexOf(proxyPrefix) !== 0) { | ||
let proxiedUrl = decodeURIComponent( | ||
_extractUrlFromReferer(proxyPrefix, req.headers.referer as string), | ||
); | ||
if (!proxiedUrl.startsWith('http://') && !proxiedUrl.startsWith('https://')) { | ||
proxiedUrl = 'http://' + proxiedUrl; | ||
} | ||
const proxiedOrigin = new URL(proxiedUrl).origin; | ||
req.url = proxyPrefix + encodeURIComponent(proxiedOrigin + req.url); | ||
} | ||
} | ||
next(); | ||
}); | ||
|
||
app.all( | ||
proxyPrefix + '*', | ||
proxy({ | ||
changeOrigin: true, | ||
logLevel: 'debug', | ||
target: 'http://127.0.0.1', | ||
|
||
router: (req: any) => { | ||
return _routePathWithReferer(proxyPrefix, req.path, req.headers.referer as string); | ||
}, | ||
|
||
pathRewrite: (_: any, req: any) => { | ||
return _rewritePath(proxyPrefix, req.path, req.query); | ||
}, | ||
}), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"baseUrl": ".", | ||
"outDir": "build/dist", | ||
"module": "esnext", | ||
"target": "es5", | ||
"lib": ["es6", "dom", "es2019"], | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"resolveJsonModule": true, | ||
"jsx": "react", | ||
"moduleResolution": "node", | ||
"noImplicitReturns": true, | ||
"noImplicitThis": true, | ||
"noImplicitAny": true, | ||
"strictBindCallApply": true, | ||
"strictNullChecks": true, | ||
"suppressImplicitAnyIndexErrors": true, | ||
"skipLibCheck": true, | ||
"esModuleInterop": true | ||
}, | ||
"exclude": ["dist", "coverage"] | ||
} |
Oops, something went wrong.