Skip to content

Commit

Permalink
feat: add threshold in DateProvider (#242)
Browse files Browse the repository at this point in the history
* Add threshold in DateProvider

* PR changes

* PR changes

* PR changes

* PR changes

* PR changes
  • Loading branch information
anku255 authored Jan 23, 2024
1 parent 416edab commit 1306e38
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 83 deletions.
2 changes: 1 addition & 1 deletion bundle/bundle.js

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions lib/build/claims/primitiveArrayClaim.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/build/claims/primitiveClaim.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions lib/build/recipeImplementation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/build/utils/dateProvider/defaultImplementation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion lib/build/utils/dateProvider/defaultImplementation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions lib/build/utils/dateProvider/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/build/utils/dateProvider/types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 68 additions & 20 deletions lib/ts/claims/primitiveArrayClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ export class PrimitiveArrayClaim<ValueType> {
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
shouldRefresh: (payload, ctx) =>
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined && payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000),
shouldRefresh: (payload, ctx) => {
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}

return (
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined &&
payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000)
);
},
validate: async (payload, ctx) => {
const claimVal = this.getValueFromPayload(payload, ctx);
if (claimVal === undefined) {
Expand Down Expand Up @@ -78,10 +88,19 @@ export class PrimitiveArrayClaim<ValueType> {
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
shouldRefresh: (payload, ctx) =>
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined && payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000),
shouldRefresh: (payload, ctx) => {
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return (
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined &&
payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000)
);
},
validate: async (payload, ctx) => {
const claimVal = this.getValueFromPayload(payload, ctx);
if (claimVal === undefined) {
Expand Down Expand Up @@ -124,10 +143,19 @@ export class PrimitiveArrayClaim<ValueType> {
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
shouldRefresh: (payload, ctx) =>
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined && payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000),
shouldRefresh: (payload, ctx) => {
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return (
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined &&
payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000)
);
},
validate: async (payload, ctx) => {
const claimVal = this.getValueFromPayload(payload, ctx);
if (claimVal === undefined) {
Expand Down Expand Up @@ -167,10 +195,20 @@ export class PrimitiveArrayClaim<ValueType> {
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
shouldRefresh: (payload, ctx) =>
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined && payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000),
shouldRefresh: (payload, ctx) => {
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}

return (
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined &&
payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000)
);
},
validate: async (payload, ctx) => {
const claimVal = this.getValueFromPayload(payload, ctx);
if (claimVal === undefined) {
Expand Down Expand Up @@ -214,10 +252,20 @@ export class PrimitiveArrayClaim<ValueType> {
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
shouldRefresh: (payload, ctx) =>
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined && payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000),
shouldRefresh: (payload, ctx) => {
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}

return (
this.getValueFromPayload(payload, ctx) === undefined ||
// We know payload[this.id] is defined since the value is not undefined in this branch
(maxAgeInSeconds !== undefined &&
payload[this.id].t < DateProvider.now() - maxAgeInSeconds * 1000)
);
},
validate: async (payload, ctx) => {
const claimVal = this.getValueFromPayload(payload, ctx);
if (claimVal === undefined) {
Expand Down
Loading

0 comments on commit 1306e38

Please sign in to comment.