Skip to content

Commit

Permalink
add disable and enable rule
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri committed Jan 9, 2025
1 parent 893a8f6 commit c479a89
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
16 changes: 16 additions & 0 deletions examples/re.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ mySdk.Rules.DeleteRule("<ruleId>", domainId, token)
.catch((error) => {
console.error(error);
});

mySdk.Rules.DisableRule("<ruleId>", domainId, token)
.then((response: any) => {
console.log("response:", response);
})
.catch((error) => {
console.error(error);
});

mySdk.Rules.EnableRule("<ruleId>", domainId, token)
.then((response: any) => {
console.log("response: ", response);
})
.catch((error) => {
console.error(error);
});
86 changes: 82 additions & 4 deletions src/re.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ export default class Rules {
* @param {string} domainId - The unique ID of the domain.
* @param {string} token - Authorization token.
* @returns {Promise<Response>} response - A promise that resolves when the rule is successfully deleted.
* @throws {Error} - If the rule status cannot be updated.
* @throws {Error} - If the rule cannot be deleted.
*/
public async DeleteRule(
ruleId: string,
domainId: string,
token: string
): Promise<Response> {
const options = {
method: "PUT",
method: "DELETE",
headers: {
"Content-Type": this.contentType,
Authorization: `Bearer ${token}`,
Expand All @@ -207,7 +207,7 @@ export default class Rules {
try {
const response = await fetch(
new URL(
`${domainId}/${this.rulesEndpoint}/${ruleId}/delete`,
`${domainId}/${this.rulesEndpoint}/${ruleId}`,
this.rulesUrl
).toString(),
options
Expand All @@ -218,11 +218,89 @@ export default class Rules {
}
const statusResponse: Response = {
status: response.status,
message: "Rule status updated successfully",
message: "Rule deleted successfully",
};
return statusResponse;
} catch (error) {
throw error;
}
}

/**
* @method EnableRule - Enables a previously disabled rule.
* @param {string} ruleId - The unique ID of the rule.
* @param {string} domainId - The unique ID of the domain.
* @param {string} token - Authorization token.
* @returns {Promise<Rule>} rule - The enabled rule object.
* @throws {Error} - If the rule cannot be enabled.
*/
public async EnableRule(
ruleId: string,
domainId: string,
token: string
): Promise<Rule> {
const options = {
method: "PUT",
headers: {
"Content-Type": this.contentType,
Authorization: `Bearer ${token}`,
},
};
try {
const response = await fetch(
new URL(
`${domainId}/${this.rulesEndpoint}/${ruleId}/enable`,
this.rulesUrl
).toString(),
options
);
if (!response.ok) {
const errorRes = await response.json();
throw Errors.HandleError(errorRes.message, response.status);
}
const enabledRule: Rule = await response.json();
return enabledRule;
} catch (error) {
throw error;
}
}

/**
* @method DisableRule - Disables a spcific rule.
* @param {string} ruleId - The unique ID of the rule.
* @param {string} domainId - The unique ID of the domain.
* @param {string} token - Authorization token.
* @returns {Promise<Rule>} rule - The disabled rule object.
* @throws {Error} - If the rule cannot be disabled.
*/
public async DisableRule(
ruleId: string,
domainId: string,
token: string
): Promise<Rule> {
const options = {
method: "PUT",
headers: {
"Content-Type": this.contentType,
Authorization: `Bearer ${token}`,
},
};
try {
const response = await fetch(
new URL(
`${domainId}/${this.rulesEndpoint}/${ruleId}/disable`,
this.rulesUrl
).toString(),
options
);
if (!response.ok) {
const errorRes = await response.json();
throw Errors.HandleError(errorRes.message, response.status);
}
const disabledRule: Rule = await response.json();
return disabledRule;
} catch (error) {
throw error;
}
}
}

0 comments on commit c479a89

Please sign in to comment.