-
Notifications
You must be signed in to change notification settings - Fork 19
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
Make it possible to use node-fetch in HTTPService #35
Open
Tim-53
wants to merge
15
commits into
main
Choose a base branch
from
feat/core/add_fetch_to_constructor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8444dfe
add optional fetch to abby class constructor, make HTTPService a norm…
Tim-53 f5f28a1
add storage service as fields
Tim-53 6bdca7e
refac code, fix tests
Tim-53 45aed8e
add send data function to core
Tim-53 114599d
use abby sendData function in react package
Tim-53 c6db324
fix next package
Tim-53 fbe46b2
fix svelte tests
Tim-53 b5f3dc8
remove HTTPService imports
Tim-53 533b20e
remove HttpService references
Tim-53 68392b5
fix next package
Tim-53 db23223
fux loadProjectData return type
Tim-53 d8168c9
Update packages/core/src/shared/http.ts
Tim-53 48acae3
Update packages/core/src/shared/http.ts
Tim-53 1510fdb
merge main
Tim-53 c775cdd
Merge branch 'feat/core/add_fetch_to_constructor' of https://github.c…
Tim-53 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,9 @@ | ||||||
import { | ||||||
ABBY_AB_STORAGE_PREFIX, | ||||||
ABBY_FF_STORAGE_PREFIX, | ||||||
AbbyDataResponse, | ||||||
} from "./shared/"; | ||||||
import { HttpService } from "./shared"; | ||||||
import { ABBY_AB_STORAGE_PREFIX, ABBY_FF_STORAGE_PREFIX, AbbyDataResponse } from "./shared/"; | ||||||
import { HttpService, AbbyEvent, AbbyEventType } from "./shared"; | ||||||
import { F } from "ts-toolbelt"; | ||||||
import { getWeightedRandomVariant } from "./mathHelpers"; | ||||||
import { parseCookies } from "./helpers"; | ||||||
import type fetch from "node-fetch"; | ||||||
|
||||||
export * from "./shared/index"; | ||||||
|
||||||
|
@@ -24,10 +21,7 @@ type Settings<FlagName extends string = string> = { | |||||
}; | ||||||
}; | ||||||
|
||||||
type LocalData< | ||||||
FlagName extends string = string, | ||||||
TestName extends string = string | ||||||
> = { | ||||||
type LocalData<FlagName extends string = string, TestName extends string = string> = { | ||||||
tests: Record< | ||||||
TestName, | ||||||
ABConfig & { | ||||||
|
@@ -61,13 +55,27 @@ export class Abby< | |||||
TestName extends string, | ||||||
Tests extends Record<string, ABConfig> | ||||||
> { | ||||||
constructor( | ||||||
private config: F.Narrow<AbbyConfig<FlagName, Tests>>, | ||||||
private persistantTestStorage?: PersistentStorage, | ||||||
private persistantFlagStorage?: PersistentStorage, | ||||||
nodeFetch?: typeof fetch | typeof globalThis.fetch | ||||||
) { | ||||||
this.#data.flags = (config.flags ?? []).reduce((acc, flag) => { | ||||||
acc[flag] = null; | ||||||
return acc; | ||||||
}, {} as any); | ||||||
this.#data.tests = config.tests ?? ({} as any); | ||||||
this.httpService = new HttpService({ fetch2: nodeFetch }); | ||||||
} | ||||||
|
||||||
private httpService; | ||||||
|
||||||
private log = (...args: any[]) => | ||||||
this.config.debug ? console.log(`core.Abby`, ...args) : () => {}; | ||||||
|
||||||
private testDevtoolOverrides: Map< | ||||||
keyof Tests, | ||||||
Tests[keyof Tests]["variants"][number] | ||||||
> = new Map(); | ||||||
private testDevtoolOverrides: Map<keyof Tests, Tests[keyof Tests]["variants"][number]> = | ||||||
new Map(); | ||||||
|
||||||
private flagDevtoolOverrides: Map<FlagName, boolean> = new Map(); | ||||||
|
||||||
|
@@ -76,30 +84,13 @@ export class Abby< | |||||
flags: {} as any, | ||||||
}; | ||||||
|
||||||
private listeners = new Set< | ||||||
(newData: LocalData<FlagName, TestName>) => void | ||||||
>(); | ||||||
private listeners = new Set<(newData: LocalData<FlagName, TestName>) => void>(); | ||||||
|
||||||
private dataInitialized: Boolean = false; | ||||||
|
||||||
private flagOverrides = new Map<string, boolean>(); | ||||||
|
||||||
private testOverrides: Map< | ||||||
keyof Tests, | ||||||
Tests[keyof Tests]["variants"][number] | ||||||
> = new Map(); | ||||||
|
||||||
constructor( | ||||||
private config: F.Narrow<AbbyConfig<FlagName, Tests>>, | ||||||
private persistantTestStorage?: PersistentStorage, | ||||||
private persistantFlagStorage?: PersistentStorage | ||||||
) { | ||||||
this.#data.flags = (config.flags ?? []).reduce((acc, flag) => { | ||||||
acc[flag] = null; | ||||||
return acc; | ||||||
}, {} as any); | ||||||
this.#data.tests = config.tests ?? ({} as any); | ||||||
} | ||||||
private testOverrides: Map<keyof Tests, Tests[keyof Tests]["variants"][number]> = new Map(); | ||||||
|
||||||
/** | ||||||
* Helper function to load the projects data from the A/BBY API | ||||||
|
@@ -108,16 +99,17 @@ export class Abby< | |||||
async loadProjectData() { | ||||||
this.log(`loadProjectData()`); | ||||||
|
||||||
const data = await HttpService.getProjectData({ | ||||||
const data = await this.httpService.getProjectData({ | ||||||
projectId: this.config.projectId, | ||||||
environment: this.config.currentEnvironment as string, | ||||||
url: this.config.apiUrl, | ||||||
}); | ||||||
if (!data) { | ||||||
this.log(`loadProjectData() => no data`); | ||||||
return; | ||||||
return null; | ||||||
} | ||||||
this.init(data); | ||||||
return data; | ||||||
} | ||||||
|
||||||
async getProjectDataAsync(): Promise<LocalData<FlagName, TestName>> { | ||||||
|
@@ -166,16 +158,13 @@ export class Abby< | |||||
this.log(`getProjectData()`); | ||||||
|
||||||
return { | ||||||
tests: Object.entries(this.#data.tests).reduce( | ||||||
(acc, [testName, test]) => { | ||||||
acc[testName as TestName] = { | ||||||
...(test as Tests[TestName]), | ||||||
selectedVariant: this.getTestVariant(testName as TestName), | ||||||
}; | ||||||
return acc; | ||||||
}, | ||||||
this.#data.tests | ||||||
), | ||||||
tests: Object.entries(this.#data.tests).reduce((acc, [testName, test]) => { | ||||||
acc[testName as TestName] = { | ||||||
...(test as Tests[TestName]), | ||||||
selectedVariant: this.getTestVariant(testName as TestName), | ||||||
}; | ||||||
return acc; | ||||||
}, this.#data.tests), | ||||||
flags: Object.keys(this.#data.flags).reduce((acc, flagName) => { | ||||||
acc[flagName as FlagName] = this.getFeatureFlag(flagName as FlagName); | ||||||
return acc; | ||||||
|
@@ -229,9 +218,7 @@ export class Abby< | |||||
* 3. DevDefault from config | ||||||
*/ | ||||||
if (process.env.NODE_ENV === "development") { | ||||||
const devOverride = (this.config.settings?.flags?.devOverrides as any)?.[ | ||||||
key | ||||||
]; | ||||||
const devOverride = (this.config.settings?.flags?.devOverrides as any)?.[key]; | ||||||
if (devOverride != null) { | ||||||
return devOverride; | ||||||
} | ||||||
|
@@ -293,10 +280,7 @@ export class Abby< | |||||
* @param key the name of the test | ||||||
* @param override the value to override the test variant with | ||||||
*/ | ||||||
updateLocalVariant<T extends keyof Tests>( | ||||||
key: T, | ||||||
override: Tests[T]["variants"][number] | ||||||
) { | ||||||
updateLocalVariant<T extends keyof Tests>(key: T, override: Tests[T]["variants"][number]) { | ||||||
this.testOverrides.set(key, override); | ||||||
this.persistantTestStorage?.set(key as string, override); | ||||||
|
||||||
|
@@ -380,4 +364,16 @@ export class Abby< | |||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
sendData({ | ||||||
url, | ||||||
type, | ||||||
data, | ||||||
}: { | ||||||
url?: string; | ||||||
type: AbbyEventType; | ||||||
data: Omit<AbbyEvent, "type">; | ||||||
}) { | ||||||
this.httpService.sendData({ url, type, data }); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constructor usally comes between the variables and the functions of a class