Skip to content
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

Functional form for autoGenCorrelationIds #3882

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
relying on third-party cookies.
* Updated sorting on grouped grids to place ungrouped items at the bottom.
* `DashCanvas` views can now be resized left and up in addition to right and down.
* `FetchService.autoGenCorrelationIds` now supports a functional form for per-request behavior.

### 🐞 Bug Fixes

Expand Down
21 changes: 13 additions & 8 deletions svc/FetchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import ShortUniqueId from 'short-unique-id';
* the most common use-cases. The Fetch API will be called with CORS enabled, credentials
* included, and redirects followed.
*
* Set {@link autoGenCorrelationIds} to true on this service to enable auto-generation of UUID
* Correlation IDs for all requests issued by this service. Can also be set on a per-request basis
* Set {@link autoGenCorrelationIds} on this service to enable auto-generation of UUID
* Correlation IDs for requests issued by this service. Can also be set on a per-request basis
* via {@link FetchOptions.correlationId}.
*
* Custom headers can be set on a request via {@link FetchOptions.headers}. Default headers for all
Expand All @@ -52,8 +52,11 @@ export class FetchService extends HoistService {
//-----------------------------------
// Public properties, Getters/Setters
//------------------------------------
/** True to auto-generate a Correlation ID for each request unless otherwise specified. */
autoGenCorrelationIds = false;
/**
* Should hoist auto-generate a Correlation ID for a request when not otherwise specified?
* Set to `true` or a dynamic per-request function to enable. Default false.
*/
autoGenCorrelationIds: boolean | ((opts: FetchOptions) => boolean) = false;

/**
* Method for generating Correlation ID's. Defaults to a 16 character random string with
Expand Down Expand Up @@ -253,10 +256,12 @@ export class FetchService extends HoistService {

// Resolve convenience options for Correlation ID to server-ready string
private withCorrelationId(opts: FetchOptions): FetchOptions {
const {correlationId} = opts;
if (isString(correlationId)) return opts;
if (correlationId === false || correlationId === null) return omit(opts, 'correlationId');
if (correlationId === true || this.autoGenCorrelationIds) {
const cid = opts.correlationId,
autoCid = this.autoGenCorrelationIds;

if (isString(cid)) return opts;
if (cid === false || cid === null) return omit(opts, 'correlationId');
if (cid === true || autoCid === true || (isFunction(autoCid) && autoCid(opts))) {
return {...opts, correlationId: this.genCorrelationId()};
}
return opts;
Expand Down
Loading