mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-01 21:13:09 +00:00
rename frontend to web and update contributing guide (#2509)
* rename frontend to web and update contributing guide * rename this time * fix build * restructure rust code * update documentation * update descriptions * Update CONTRIBUTING.md Co-authored-by: J H <2364004+Blu-J@users.noreply.github.com> --------- Co-authored-by: Aiden McClelland <me@drbonez.dev> Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com> Co-authored-by: J H <2364004+Blu-J@users.noreply.github.com>
This commit is contained in:
17
web/projects/ui/src/app/util/animations.ts
Normal file
17
web/projects/ui/src/app/util/animations.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { animate, style, transition, trigger } from '@angular/animations'
|
||||
|
||||
const TRANSITION = '{{duration}}ms {{delay}}ms ease-in-out'
|
||||
const DURATION = { params: { duration: 300, delay: 0 } }
|
||||
|
||||
export const heightCollapse = trigger('heightCollapse', [
|
||||
transition(
|
||||
':enter',
|
||||
[style({ height: 0 }), animate(TRANSITION, style({ height: '*' }))],
|
||||
DURATION,
|
||||
),
|
||||
transition(
|
||||
':leave',
|
||||
[style({ height: '*' }), animate(TRANSITION, style({ height: 0 }))],
|
||||
DURATION,
|
||||
),
|
||||
])
|
||||
252
web/projects/ui/src/app/util/countries.json
Normal file
252
web/projects/ui/src/app/util/countries.json
Normal file
@@ -0,0 +1,252 @@
|
||||
{
|
||||
"AD": "Andorra",
|
||||
"AE": "United Arab Emirates",
|
||||
"AF": "Afghanistan",
|
||||
"AG": "Antigua and Barbuda",
|
||||
"AI": "Anguilla",
|
||||
"AL": "Albania",
|
||||
"AM": "Armenia",
|
||||
"AO": "Angola",
|
||||
"AQ": "Antarctica",
|
||||
"AR": "Argentina",
|
||||
"AS": "American Samoa",
|
||||
"AT": "Austria",
|
||||
"AU": "Australia",
|
||||
"AW": "Aruba",
|
||||
"AX": "Aland Islands",
|
||||
"AZ": "Azerbaijan",
|
||||
"BA": "Bosnia and Herzegovina",
|
||||
"BB": "Barbados",
|
||||
"BD": "Bangladesh",
|
||||
"BE": "Belgium",
|
||||
"BF": "Burkina Faso",
|
||||
"BG": "Bulgaria",
|
||||
"BH": "Bahrain",
|
||||
"BI": "Burundi",
|
||||
"BJ": "Benin",
|
||||
"BL": "Saint Barthelemy",
|
||||
"BM": "Bermuda",
|
||||
"BN": "Brunei",
|
||||
"BO": "Bolivia",
|
||||
"BQ": "Bonaire, Saint Eustatius and Saba ",
|
||||
"BR": "Brazil",
|
||||
"BS": "Bahamas",
|
||||
"BT": "Bhutan",
|
||||
"BV": "Bouvet Island",
|
||||
"BW": "Botswana",
|
||||
"BY": "Belarus",
|
||||
"BZ": "Belize",
|
||||
"CA": "Canada",
|
||||
"CC": "Cocos Islands",
|
||||
"CD": "Democratic Republic of the Congo",
|
||||
"CF": "Central African Republic",
|
||||
"CG": "Republic of the Congo",
|
||||
"CH": "Switzerland",
|
||||
"CI": "Ivory Coast",
|
||||
"CK": "Cook Islands",
|
||||
"CL": "Chile",
|
||||
"CM": "Cameroon",
|
||||
"CN": "China",
|
||||
"CO": "Colombia",
|
||||
"CR": "Costa Rica",
|
||||
"CU": "Cuba",
|
||||
"CV": "Cape Verde",
|
||||
"CW": "Curacao",
|
||||
"CX": "Christmas Island",
|
||||
"CY": "Cyprus",
|
||||
"CZ": "Czech Republic",
|
||||
"DE": "Germany",
|
||||
"DJ": "Djibouti",
|
||||
"DK": "Denmark",
|
||||
"DM": "Dominica",
|
||||
"DO": "Dominican Republic",
|
||||
"DZ": "Algeria",
|
||||
"EC": "Ecuador",
|
||||
"EE": "Estonia",
|
||||
"EG": "Egypt",
|
||||
"EH": "Western Sahara",
|
||||
"ER": "Eritrea",
|
||||
"ES": "Spain",
|
||||
"ET": "Ethiopia",
|
||||
"FI": "Finland",
|
||||
"FJ": "Fiji",
|
||||
"FK": "Falkland Islands",
|
||||
"FM": "Micronesia",
|
||||
"FO": "Faroe Islands",
|
||||
"FR": "France",
|
||||
"GA": "Gabon",
|
||||
"GB": "United Kingdom",
|
||||
"GD": "Grenada",
|
||||
"GE": "Georgia",
|
||||
"GF": "French Guiana",
|
||||
"GG": "Guernsey",
|
||||
"GH": "Ghana",
|
||||
"GI": "Gibraltar",
|
||||
"GL": "Greenland",
|
||||
"GM": "Gambia",
|
||||
"GN": "Guinea",
|
||||
"GP": "Guadeloupe",
|
||||
"GQ": "Equatorial Guinea",
|
||||
"GR": "Greece",
|
||||
"GS": "South Georgia and the South Sandwich Islands",
|
||||
"GT": "Guatemala",
|
||||
"GU": "Guam",
|
||||
"GW": "Guinea-Bissau",
|
||||
"GY": "Guyana",
|
||||
"HK": "Hong Kong",
|
||||
"HM": "Heard Island and McDonald Islands",
|
||||
"HN": "Honduras",
|
||||
"HR": "Croatia",
|
||||
"HT": "Haiti",
|
||||
"HU": "Hungary",
|
||||
"ID": "Indonesia",
|
||||
"IE": "Ireland",
|
||||
"IL": "Israel",
|
||||
"IM": "Isle of Man",
|
||||
"IN": "India",
|
||||
"IO": "British Indian Ocean Territory",
|
||||
"IQ": "Iraq",
|
||||
"IR": "Iran",
|
||||
"IS": "Iceland",
|
||||
"IT": "Italy",
|
||||
"JE": "Jersey",
|
||||
"JM": "Jamaica",
|
||||
"JO": "Jordan",
|
||||
"JP": "Japan",
|
||||
"KE": "Kenya",
|
||||
"KG": "Kyrgyzstan",
|
||||
"KH": "Cambodia",
|
||||
"KI": "Kiribati",
|
||||
"KM": "Comoros",
|
||||
"KN": "Saint Kitts and Nevis",
|
||||
"KP": "North Korea",
|
||||
"KR": "South Korea",
|
||||
"KW": "Kuwait",
|
||||
"KY": "Cayman Islands",
|
||||
"KZ": "Kazakhstan",
|
||||
"LA": "Laos",
|
||||
"LB": "Lebanon",
|
||||
"LC": "Saint Lucia",
|
||||
"LI": "Liechtenstein",
|
||||
"LK": "Sri Lanka",
|
||||
"LR": "Liberia",
|
||||
"LS": "Lesotho",
|
||||
"LT": "Lithuania",
|
||||
"LU": "Luxembourg",
|
||||
"LV": "Latvia",
|
||||
"LY": "Libya",
|
||||
"MA": "Morocco",
|
||||
"MC": "Monaco",
|
||||
"MD": "Moldova",
|
||||
"ME": "Montenegro",
|
||||
"MF": "Saint Martin",
|
||||
"MG": "Madagascar",
|
||||
"MH": "Marshall Islands",
|
||||
"MK": "Macedonia",
|
||||
"ML": "Mali",
|
||||
"MM": "Myanmar",
|
||||
"MN": "Mongolia",
|
||||
"MO": "Macao",
|
||||
"MP": "Northern Mariana Islands",
|
||||
"MQ": "Martinique",
|
||||
"MR": "Mauritania",
|
||||
"MS": "Montserrat",
|
||||
"MT": "Malta",
|
||||
"MU": "Mauritius",
|
||||
"MV": "Maldives",
|
||||
"MW": "Malawi",
|
||||
"MX": "Mexico",
|
||||
"MY": "Malaysia",
|
||||
"MZ": "Mozambique",
|
||||
"NA": "Namibia",
|
||||
"NC": "New Caledonia",
|
||||
"NE": "Niger",
|
||||
"NF": "Norfolk Island",
|
||||
"NG": "Nigeria",
|
||||
"NI": "Nicaragua",
|
||||
"NL": "Netherlands",
|
||||
"NO": "Norway",
|
||||
"NP": "Nepal",
|
||||
"NR": "Nauru",
|
||||
"NU": "Niue",
|
||||
"NZ": "New Zealand",
|
||||
"OM": "Oman",
|
||||
"PA": "Panama",
|
||||
"PE": "Peru",
|
||||
"PF": "French Polynesia",
|
||||
"PG": "Papua New Guinea",
|
||||
"PH": "Philippines",
|
||||
"PK": "Pakistan",
|
||||
"PL": "Poland",
|
||||
"PM": "Saint Pierre and Miquelon",
|
||||
"PN": "Pitcairn",
|
||||
"PR": "Puerto Rico",
|
||||
"PS": "Palestinian Territory",
|
||||
"PT": "Portugal",
|
||||
"PW": "Palau",
|
||||
"PY": "Paraguay",
|
||||
"QA": "Qatar",
|
||||
"RE": "Reunion",
|
||||
"RO": "Romania",
|
||||
"RS": "Serbia",
|
||||
"RU": "Russia",
|
||||
"RW": "Rwanda",
|
||||
"SA": "Saudi Arabia",
|
||||
"SB": "Solomon Islands",
|
||||
"SC": "Seychelles",
|
||||
"SD": "Sudan",
|
||||
"SE": "Sweden",
|
||||
"SG": "Singapore",
|
||||
"SH": "Saint Helena",
|
||||
"SI": "Slovenia",
|
||||
"SJ": "Svalbard and Jan Mayen",
|
||||
"SK": "Slovakia",
|
||||
"SL": "Sierra Leone",
|
||||
"SM": "San Marino",
|
||||
"SN": "Senegal",
|
||||
"SO": "Somalia",
|
||||
"SR": "Suriname",
|
||||
"SS": "South Sudan",
|
||||
"ST": "Sao Tome and Principe",
|
||||
"SV": "El Salvador",
|
||||
"SX": "Sint Maarten",
|
||||
"SY": "Syria",
|
||||
"SZ": "Swaziland",
|
||||
"TC": "Turks and Caicos Islands",
|
||||
"TD": "Chad",
|
||||
"TF": "French Southern Territories",
|
||||
"TG": "Togo",
|
||||
"TH": "Thailand",
|
||||
"TJ": "Tajikistan",
|
||||
"TK": "Tokelau",
|
||||
"TL": "East Timor",
|
||||
"TM": "Turkmenistan",
|
||||
"TN": "Tunisia",
|
||||
"TO": "Tonga",
|
||||
"TR": "Turkey",
|
||||
"TT": "Trinidad and Tobago",
|
||||
"TV": "Tuvalu",
|
||||
"TW": "Taiwan",
|
||||
"TZ": "Tanzania",
|
||||
"UA": "Ukraine",
|
||||
"UG": "Uganda",
|
||||
"UM": "United States Minor Outlying Islands",
|
||||
"US": "United States",
|
||||
"UY": "Uruguay",
|
||||
"UZ": "Uzbekistan",
|
||||
"VA": "Vatican",
|
||||
"VC": "Saint Vincent and the Grenadines",
|
||||
"VE": "Venezuela",
|
||||
"VG": "British Virgin Islands",
|
||||
"VI": "U.S. Virgin Islands",
|
||||
"VN": "Vietnam",
|
||||
"VU": "Vanuatu",
|
||||
"WF": "Wallis and Futuna",
|
||||
"WS": "Samoa",
|
||||
"XK": "Kosovo",
|
||||
"YE": "Yemen",
|
||||
"YT": "Mayotte",
|
||||
"ZA": "South Africa",
|
||||
"ZM": "Zambia",
|
||||
"ZW": "Zimbabwe"
|
||||
}
|
||||
17
web/projects/ui/src/app/util/dry-update.ts
Normal file
17
web/projects/ui/src/app/util/dry-update.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Emver } from '@start9labs/shared'
|
||||
import { DataModel } from '../services/patch-db/data-model'
|
||||
|
||||
export function dryUpdate(
|
||||
{ id, version }: { id: string; version: string },
|
||||
pkgs: DataModel['package-data'],
|
||||
emver: Emver,
|
||||
): string[] {
|
||||
return Object.values(pkgs)
|
||||
.filter(
|
||||
pkg =>
|
||||
Object.keys(pkg.installed?.['current-dependencies'] || {}).some(
|
||||
pkgId => pkgId === id,
|
||||
) && !emver.satisfies(version, pkg.manifest.dependencies[id].version),
|
||||
)
|
||||
.map(pkg => pkg.manifest.title)
|
||||
}
|
||||
19
web/projects/ui/src/app/util/get-package-data.ts
Normal file
19
web/projects/ui/src/app/util/get-package-data.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { PatchDB } from 'patch-db-client'
|
||||
import {
|
||||
DataModel,
|
||||
PackageDataEntry,
|
||||
} from 'src/app/services/patch-db/data-model'
|
||||
import { firstValueFrom } from 'rxjs'
|
||||
|
||||
export async function getPackage(
|
||||
patch: PatchDB<DataModel>,
|
||||
id: string,
|
||||
): Promise<PackageDataEntry | undefined> {
|
||||
return firstValueFrom(patch.watch$('package-data', id))
|
||||
}
|
||||
|
||||
export async function getAllPackages(
|
||||
patch: PatchDB<DataModel>,
|
||||
): Promise<DataModel['package-data']> {
|
||||
return firstValueFrom(patch.watch$('package-data'))
|
||||
}
|
||||
48
web/projects/ui/src/app/util/get-package-info.ts
Normal file
48
web/projects/ui/src/app/util/get-package-info.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { PackageDataEntry } from '../services/patch-db/data-model'
|
||||
import {
|
||||
DependencyStatus,
|
||||
HealthStatus,
|
||||
PrimaryRendering,
|
||||
PrimaryStatus,
|
||||
renderPkgStatus,
|
||||
StatusRendering,
|
||||
} from '../services/pkg-status-rendering.service'
|
||||
import { ProgressData } from 'src/app/types/progress-data'
|
||||
import { Subscription } from 'rxjs'
|
||||
import { packageLoadingProgress } from './package-loading-progress'
|
||||
import { PkgDependencyErrors } from '../services/dep-error.service'
|
||||
|
||||
export function getPackageInfo(
|
||||
entry: PackageDataEntry,
|
||||
depErrors: PkgDependencyErrors,
|
||||
): PkgInfo {
|
||||
const statuses = renderPkgStatus(entry, depErrors)
|
||||
const primaryRendering = PrimaryRendering[statuses.primary]
|
||||
|
||||
return {
|
||||
entry,
|
||||
primaryRendering,
|
||||
primaryStatus: statuses.primary,
|
||||
installProgress: packageLoadingProgress(entry['install-progress']),
|
||||
error:
|
||||
statuses.health === HealthStatus.Failure ||
|
||||
statuses.dependency === DependencyStatus.Warning,
|
||||
warning: statuses.primary === PrimaryStatus.NeedsConfig,
|
||||
transitioning:
|
||||
primaryRendering.showDots ||
|
||||
statuses.health === HealthStatus.Waiting ||
|
||||
statuses.health === HealthStatus.Loading ||
|
||||
statuses.health === HealthStatus.Starting,
|
||||
}
|
||||
}
|
||||
|
||||
export interface PkgInfo {
|
||||
entry: PackageDataEntry
|
||||
primaryRendering: StatusRendering
|
||||
primaryStatus: PrimaryStatus
|
||||
installProgress: ProgressData | null
|
||||
error: boolean
|
||||
warning: boolean
|
||||
transitioning: boolean
|
||||
sub?: Subscription | null
|
||||
}
|
||||
11
web/projects/ui/src/app/util/get-project-id.ts
Normal file
11
web/projects/ui/src/app/util/get-project-id.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { ActivatedRoute } from '@angular/router'
|
||||
|
||||
export function getProjectId({ snapshot }: ActivatedRoute): string {
|
||||
const projectId = snapshot.paramMap.get('projectId')
|
||||
|
||||
if (!projectId) {
|
||||
throw new Error('projectId is missing from route params')
|
||||
}
|
||||
|
||||
return projectId
|
||||
}
|
||||
9
web/projects/ui/src/app/util/get-server-info.ts
Normal file
9
web/projects/ui/src/app/util/get-server-info.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { PatchDB } from 'patch-db-client'
|
||||
import { DataModel, ServerInfo } from 'src/app/services/patch-db/data-model'
|
||||
import { firstValueFrom } from 'rxjs'
|
||||
|
||||
export async function getServerInfo(
|
||||
patch: PatchDB<DataModel>,
|
||||
): Promise<ServerInfo> {
|
||||
return firstValueFrom(patch.watch$('server-info'))
|
||||
}
|
||||
7
web/projects/ui/src/app/util/has-deps.ts
Normal file
7
web/projects/ui/src/app/util/has-deps.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { PackageDataEntry } from '../services/patch-db/data-model'
|
||||
|
||||
export function hasCurrentDeps(pkg: PackageDataEntry): boolean {
|
||||
return !!Object.keys(pkg.installed?.['current-dependents'] || {}).filter(
|
||||
depId => depId !== pkg.manifest.id,
|
||||
).length
|
||||
}
|
||||
50
web/projects/ui/src/app/util/package-loading-progress.ts
Normal file
50
web/projects/ui/src/app/util/package-loading-progress.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { isEmptyObject } from '@start9labs/shared'
|
||||
import { ProgressData } from 'src/app/types/progress-data'
|
||||
import { InstallProgress } from '../services/patch-db/data-model'
|
||||
|
||||
export function packageLoadingProgress(
|
||||
loadData?: InstallProgress,
|
||||
): ProgressData | null {
|
||||
if (!loadData || isEmptyObject(loadData)) {
|
||||
return null
|
||||
}
|
||||
|
||||
let {
|
||||
downloaded,
|
||||
validated,
|
||||
unpacked,
|
||||
size,
|
||||
'download-complete': downloadComplete,
|
||||
'validation-complete': validationComplete,
|
||||
'unpack-complete': unpackComplete,
|
||||
} = loadData
|
||||
|
||||
// only permit 100% when "complete" == true
|
||||
size = size || 0
|
||||
downloaded = downloadComplete ? size : Math.max(downloaded - 1, 0)
|
||||
validated = validationComplete ? size : Math.max(validated - 1, 0)
|
||||
unpacked = unpackComplete ? size : Math.max(unpacked - 1, 0)
|
||||
|
||||
const downloadWeight = 1
|
||||
const validateWeight = 0.2
|
||||
const unpackWeight = 0.7
|
||||
|
||||
const numerator = Math.floor(
|
||||
downloadWeight * downloaded +
|
||||
validateWeight * validated +
|
||||
unpackWeight * unpacked,
|
||||
)
|
||||
|
||||
const denominator = Math.floor(
|
||||
size * (downloadWeight + validateWeight + unpackWeight),
|
||||
)
|
||||
const totalProgress = Math.floor((100 * numerator) / denominator)
|
||||
|
||||
return {
|
||||
totalProgress,
|
||||
downloadProgress: Math.floor((100 * downloaded) / size),
|
||||
validateProgress: Math.floor((100 * validated) / size),
|
||||
unpackProgress: Math.floor((100 * unpacked) / size),
|
||||
isComplete: downloadComplete && validationComplete && unpackComplete,
|
||||
}
|
||||
}
|
||||
152
web/projects/ui/src/app/util/properties.util.ts
Normal file
152
web/projects/ui/src/app/util/properties.util.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { applyOperation } from 'fast-json-patch'
|
||||
import matches, {
|
||||
Parser,
|
||||
shape,
|
||||
string,
|
||||
literal,
|
||||
boolean,
|
||||
deferred,
|
||||
dictionary,
|
||||
anyOf,
|
||||
number,
|
||||
arrayOf,
|
||||
} from 'ts-matches'
|
||||
|
||||
type ValidVersion = 1 | 2
|
||||
|
||||
type PropertiesV1 = typeof matchPropertiesV1._TYPE
|
||||
type PackagePropertiesV1 = PropertiesV1[]
|
||||
type PackagePropertiesV2 = {
|
||||
[name: string]: PackagePropertyString | PackagePropertyObject
|
||||
}
|
||||
type PackagePropertiesVersionedData<T extends number> = T extends 1
|
||||
? PackagePropertiesV1
|
||||
: T extends 2
|
||||
? PackagePropertiesV2
|
||||
: never
|
||||
|
||||
type PackagePropertyString = typeof matchPackagePropertyString._TYPE
|
||||
|
||||
export type PackagePropertiesVersioned<T extends number> = {
|
||||
version: T
|
||||
data: PackagePropertiesVersionedData<T>
|
||||
}
|
||||
export type PackageProperties = PackagePropertiesV2
|
||||
|
||||
const matchPropertiesV1 = shape(
|
||||
{
|
||||
name: string,
|
||||
value: string,
|
||||
description: string,
|
||||
copyable: boolean,
|
||||
qr: boolean,
|
||||
},
|
||||
['description', 'copyable', 'qr'],
|
||||
{ copyable: false, qr: false } as const,
|
||||
)
|
||||
|
||||
const [matchPackagePropertiesV2, setPPV2] = deferred<PackagePropertiesV2>()
|
||||
const matchPackagePropertyString = shape(
|
||||
{
|
||||
type: literal('string'),
|
||||
description: string,
|
||||
value: string,
|
||||
copyable: boolean,
|
||||
qr: boolean,
|
||||
masked: boolean,
|
||||
},
|
||||
['description', 'copyable', 'qr', 'masked'],
|
||||
{
|
||||
copyable: false,
|
||||
qr: false,
|
||||
masked: false,
|
||||
} as const,
|
||||
)
|
||||
const matchPackagePropertyObject = shape(
|
||||
{
|
||||
type: literal('object'),
|
||||
value: matchPackagePropertiesV2,
|
||||
description: string,
|
||||
},
|
||||
['description'],
|
||||
)
|
||||
|
||||
const matchPropertyV2 = anyOf(
|
||||
matchPackagePropertyString,
|
||||
matchPackagePropertyObject,
|
||||
)
|
||||
type PackagePropertyObject = typeof matchPackagePropertyObject._TYPE
|
||||
setPPV2(dictionary([string, matchPropertyV2]))
|
||||
|
||||
const matchPackagePropertiesVersionedV1 = shape({
|
||||
version: number,
|
||||
data: arrayOf(matchPropertiesV1),
|
||||
})
|
||||
const matchPackagePropertiesVersionedV2 = shape({
|
||||
version: number,
|
||||
data: dictionary([string, matchPropertyV2]),
|
||||
})
|
||||
|
||||
export function parsePropertiesPermissive(
|
||||
properties: unknown,
|
||||
errorCallback: (err: Error) => any = console.warn,
|
||||
): PackageProperties {
|
||||
return matches(properties)
|
||||
.when(matchPackagePropertiesVersionedV1, prop =>
|
||||
parsePropertiesV1Permissive(prop.data, errorCallback),
|
||||
)
|
||||
.when(matchPackagePropertiesVersionedV2, prop => prop.data)
|
||||
.when(matches.nill, {})
|
||||
.defaultToLazy(() => {
|
||||
errorCallback(new TypeError(`value is not valid`))
|
||||
return {}
|
||||
})
|
||||
}
|
||||
|
||||
function parsePropertiesV1Permissive(
|
||||
properties: unknown,
|
||||
errorCallback: (err: Error) => any,
|
||||
): PackageProperties {
|
||||
if (!Array.isArray(properties)) {
|
||||
errorCallback(new TypeError(`${properties} is not an array`))
|
||||
return {}
|
||||
}
|
||||
return properties.reduce(
|
||||
(prev: PackagePropertiesV2, cur: unknown, idx: number) => {
|
||||
const result = matchPropertiesV1.enumParsed(cur)
|
||||
if ('value' in result) {
|
||||
const value = result.value
|
||||
prev[value.name] = {
|
||||
type: 'string',
|
||||
value: value.value,
|
||||
description: value.description,
|
||||
copyable: value.copyable,
|
||||
qr: value.qr,
|
||||
masked: false,
|
||||
}
|
||||
} else {
|
||||
const error = result.error
|
||||
const message = Parser.validatorErrorAsString(error)
|
||||
const dataPath = error.keys.map(removeQuotes).join('/')
|
||||
errorCallback(new Error(`/data/${idx}: ${message}`))
|
||||
if (dataPath) {
|
||||
applyOperation(cur, {
|
||||
op: 'replace',
|
||||
path: `/${dataPath}`,
|
||||
value: undefined,
|
||||
})
|
||||
}
|
||||
}
|
||||
return prev
|
||||
},
|
||||
{},
|
||||
)
|
||||
}
|
||||
|
||||
const removeRegex = /('|")/
|
||||
function removeQuotes(x: string) {
|
||||
while (removeRegex.test(x)) {
|
||||
x = x.replace(removeRegex, '')
|
||||
}
|
||||
return x
|
||||
}
|
||||
95
web/projects/ui/src/app/util/rxjs.util.ts
Normal file
95
web/projects/ui/src/app/util/rxjs.util.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import {
|
||||
Observable,
|
||||
from,
|
||||
interval,
|
||||
race,
|
||||
OperatorFunction,
|
||||
Observer,
|
||||
} from 'rxjs'
|
||||
import { take, map } from 'rxjs/operators'
|
||||
|
||||
export function fromAsync$<S, T>(
|
||||
async: (s: S) => Promise<T>,
|
||||
s: S,
|
||||
): Observable<T>
|
||||
export function fromAsync$<T>(async: () => Promise<T>): Observable<T>
|
||||
export function fromAsync$<S, T>(
|
||||
async: (s: S) => Promise<T>,
|
||||
s?: S,
|
||||
): Observable<T> {
|
||||
return from(async(s as S))
|
||||
}
|
||||
|
||||
export function fromAsyncP<T>(async: () => Promise<T>): Promise<T>
|
||||
export function fromAsyncP<S, T>(
|
||||
async: (s: S) => Promise<T>,
|
||||
s?: S,
|
||||
): Promise<T> {
|
||||
return async(s as S)
|
||||
}
|
||||
|
||||
// emits + completes after ms
|
||||
export function emitAfter$(ms: number): Observable<number> {
|
||||
return interval(ms).pipe(take(1))
|
||||
}
|
||||
|
||||
// throws unless source observable emits withing timeout
|
||||
export function throwIn<T>(timeout: number): OperatorFunction<T, T> {
|
||||
return o =>
|
||||
race(
|
||||
o,
|
||||
emitAfter$(timeout).pipe(
|
||||
map(() => {
|
||||
throw new Error('timeout')
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// o.pipe(squash) : Observable<void> regardless of o emission type.
|
||||
export const squash = map(() => {})
|
||||
|
||||
/*
|
||||
The main purpose of fromSync$ is to normalize error handling during a sequence
|
||||
of actions beginning with a standard synchronous action and followed by a pipe.
|
||||
For example, imagine we have `f(s: S): T` which might throw, and we wish to define the following:
|
||||
```
|
||||
function observableF(t: T): Observable<any> {
|
||||
const s = f(t)
|
||||
return someFunctionReturningAnObservable(s)
|
||||
}
|
||||
```
|
||||
|
||||
For the caller, `observableF(t).pipe(...).subscribe({ error: e => console.error('observable errored!') })`
|
||||
might throw an error from `f` which does not result in 'observable errored!' being logged.
|
||||
We could fix this with...
|
||||
```
|
||||
function observableF(t: T): Observable<any> {
|
||||
try {
|
||||
const s = f(t)
|
||||
return someFunctionReturningAnObservable(s)
|
||||
} catch(e) {
|
||||
return throwError(e)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
or we could use fromSync as below
|
||||
```
|
||||
function observableF(t: T): Observable<any> {
|
||||
return fromSync$(f, t).concatMap(someFunctionReturningAnObservable)
|
||||
}
|
||||
```
|
||||
*/
|
||||
export function fromSync$<S, T>(sync: (s: S) => T, s: S): Observable<T>
|
||||
export function fromSync$<T>(sync: () => T): Observable<T>
|
||||
export function fromSync$<S, T>(sync: (s: S) => T, s?: S): Observable<T> {
|
||||
return new Observable((subscriber: Observer<T>) => {
|
||||
try {
|
||||
subscriber.next(sync(s as S))
|
||||
subscriber.complete()
|
||||
} catch (e) {
|
||||
subscriber.error(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
4
web/projects/ui/src/app/util/web.util.ts
Normal file
4
web/projects/ui/src/app/util/web.util.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function strip(html: string) {
|
||||
let doc = new DOMParser().parseFromString(html, 'text/html')
|
||||
return doc.body.textContent || ''
|
||||
}
|
||||
Reference in New Issue
Block a user