From 24dc9f0510d5ef6e3da157c0efd1ceb73d4ed7da Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 3 May 2023 16:59:05 -0600 Subject: [PATCH] fix export interface stuff --- lib/mainFn/NetworkInterfaceBuilder.ts | 35 ++++++++++++++------------- lib/mainFn/Origin.ts | 29 +++++++++++++--------- lib/test/configBuilder.test.ts | 2 -- lib/types.ts | 30 +++++++---------------- 4 files changed, 45 insertions(+), 51 deletions(-) diff --git a/lib/mainFn/NetworkInterfaceBuilder.ts b/lib/mainFn/NetworkInterfaceBuilder.ts index 09bc68f..cdd0a64 100644 --- a/lib/mainFn/NetworkInterfaceBuilder.ts +++ b/lib/mainFn/NetworkInterfaceBuilder.ts @@ -21,9 +21,9 @@ export class NetworkInterfaceBuilder { id: string description: string ui: boolean - basic?: null | { password: null | string; username: string } - path?: string - search?: Record + basic: null | { password: null | string; username: string } + path: string + search: Record }, ) {} @@ -35,20 +35,21 @@ export class NetworkInterfaceBuilder { * @param addresses * @returns */ - async exportAddresses(addresses: Iterable) { - const { name, description, id, ui, path, search } = this.options - for (const origin of addresses) { - const address = origin.withAuth(this.options.basic) - await this.options.effects.exportAddress({ - name, - description, - address, - id, - ui, - path, - search, - }) - } + async export(origins: Iterable) { + const { name, description, id, ui, basic, path, search } = this.options + + const addresses = Array.from(origins).map((o) => + o.build({ basic, path, search }), + ) + + await this.options.effects.exportNetworkInterface({ + id, + name, + description, + addresses, + ui, + }) + return {} as AddressReceipt } } diff --git a/lib/mainFn/Origin.ts b/lib/mainFn/Origin.ts index 9e28d76..7d1ca02 100644 --- a/lib/mainFn/Origin.ts +++ b/lib/mainFn/Origin.ts @@ -1,19 +1,26 @@ export class Origin { constructor(readonly protocol: string | null, readonly host: string) {} - withAuth( - origin?: - | { - password: null | string - username: string - } - | null - | undefined, - ) { + build({ basic, path, search }: BuildOptions) { // prettier-ignore - const urlAuth = !!(origin) ? `${origin.username}${origin.password != null ?`:${origin.password}`:''}@` : + const urlAuth = !!(basic) ? `${basic.username}${basic.password != null ?`:${basic.password}`:''}@` : ''; const protocolSection = this.protocol != null ? `${this.protocol}://` : "" - return `${protocolSection}${urlAuth}${this.host}` + + const qpEntries = Object.entries(search) + .map( + ([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`, + ) + .join("&") + + const qp = qpEntries.length ? `?${qpEntries}` : "" + + return `${protocolSection}${urlAuth}${this.host}${path}${qp}` } } + +type BuildOptions = { + basic: { username: string; password: string | null } | null + path: string + search: Record +} diff --git a/lib/test/configBuilder.test.ts b/lib/test/configBuilder.test.ts index 5798eca..8bb488b 100644 --- a/lib/test/configBuilder.test.ts +++ b/lib/test/configBuilder.test.ts @@ -541,8 +541,6 @@ describe("values", () => { Test, | { unionSelectKey: "a"; unionValueKey: { b: boolean } } | { unionSelectKey: "b"; unionValueKey: { b: boolean } } - | null - | undefined >()(null) const built = await value.build({} as any) diff --git a/lib/types.ts b/lib/types.ts index 98729e1..d6657aa 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -165,30 +165,18 @@ export type ActionMetaData = { group?: string } -export type AddressType = { +export type NetworkInterface = { + id: string /** The title of this field to be dsimplayed */ name: string /** Human readable description, used as tooltip usually */ description: string - /** URI location */ - address: string - id: string + /** All URIs */ + addresses: string[] /** Defaults to false, but describes if this address can be opened in a browser as an * ui interface */ ui?: boolean - /** - * The id is that a path will create a link in the ui that can go to specific pages, like - * admin, or settings, or something like that. - * Default = '' - */ - path?: string - /** - * This is the query params in the url, and is a map of key value pairs - * Default = {} - * if empty then will not be added to the url - */ - search?: Record } /** Used to reach out from the pure js runtime */ @@ -376,7 +364,7 @@ export type Effects = { /** When we want to create a link in the front end interfaces, and example is * exposing a url to view a web service */ - exportAddress(options: AddressType): Promise + exportNetworkInterface(options: NetworkInterface): Promise /** * There are times that we want to see the addresses that where exported @@ -384,10 +372,10 @@ export type Effects = { * * Note: any auth should be filtered out already */ - getAddresses(options: { - id?: PackageId - addressId?: string - }): Promise + getInterface(options: { + serviceId?: PackageId + interfaceId?: string + }): Promise /** *Remove an address that was exported. Used problably during main or during setConfig.