Skip to content

Commit

Permalink
Add expiresAt to updateLicenseKey function (#81)
Browse files Browse the repository at this point in the history
* feat: add `expreisAt` to updateLicenseKey function

* docs: update change description
  • Loading branch information
keyding authored Apr 19, 2024
1 parent b4d071a commit 218d2ef
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-kangaroos-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lemonsqueezy/lemonsqueezy.js": patch
---

Add `expiresAt` to `updateLicenseKey` function
5 changes: 3 additions & 2 deletions src/licenseKeys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function listLicenseKeys(params: ListLicenseKeysParams = {}) {
* @param licenseKeyId The license key id.
* @param licenseKey (Optional) Values to be updated.
* @param [licenseKey.activationLimit] (Optional) The activation limit of this license key. Assign `null` to set the activation limit to "unlimited".
* @param [licenseKey.expiresAt] (Optional) An [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formatted date-time string indicating when the license key expires. Can be `null` if the license key is perpetual.
* @param [licenseKey.disabled] (Optional) If `true`, the license key will have "disabled" status.
* @returns A license key object.
*/
Expand All @@ -67,8 +68,8 @@ export function updateLicenseKey(
) {
requiredCheck({ licenseKeyId });

const { activationLimit, disabled = false } = licenseKey;
const attributes = convertKeys({ activationLimit, disabled });
const { activationLimit, disabled = false, expiresAt } = licenseKey;
const attributes = convertKeys({ activationLimit, disabled, expiresAt });

return $fetch<LicenseKey>({
path: `/v1/license-keys/${licenseKeyId}`,
Expand Down
4 changes: 4 additions & 0 deletions src/licenseKeys/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export type UpdateLicenseKey = {
* The activation limit of this license key. Assign `null` to set the activation limit to "unlimited".
*/
activationLimit?: number | null;
/**
* An [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formatted date-time string indicating when the license key expires. Can be `null` if the license key is perpetual.
*/
expiresAt?: string | null;
/**
* If `true`, the license key will have "disabled" status.
*/
Expand Down
185 changes: 184 additions & 1 deletion test/licenseKeys/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,14 @@ describe("Update a license key", () => {
}
});

it("Should return a license key object with activation_limit is 5", async () => {
it("Should return a license key object with activation_limit is 5 and disabled is false", async () => {
const newActivationLimit = 5;
const {
statusCode,
error,
data: _data,
} = await updateLicenseKey(licenseKeyId, {
disabled: false,
activationLimit: newActivationLimit,
});
expect(statusCode).toEqual(200);
Expand Down Expand Up @@ -536,6 +537,188 @@ describe("Update a license key", () => {
expect(Object.keys(relationships).length).toEqual(relationshipItems.length);
});

it("Should return a license key object with expires_at is null", async () => {
const {
statusCode,
error,
data: _data,
} = await updateLicenseKey(licenseKeyId, {
expiresAt: null,
});
expect(statusCode).toEqual(200);
expect(error).toBeNull();
expect(_data).toBeDefined();

const { data, links } = _data!;
expect(links.self).toEqual(`${API_BASE_URL}${PATH}${licenseKeyId}`);

const { type, id, attributes, relationships } = data;
expect(type).toEqual(DATA_TYPE);
expect(id).toEqual(licenseKeyId.toString());
expect(attributes).toBeDefined();
expect(relationships).toBeDefined();

const {
store_id,
customer_id,
order_id,
order_item_id,
product_id,
status,
key,
user_name,
user_email,
key_short,
activation_limit,
instances_count,
disabled,
status_formatted,
expires_at,
created_at,
updated_at,
test_mode,
} = attributes;
const items = [
store_id,
customer_id,
order_id,
order_item_id,
product_id,
status,
key,
user_name,
user_email,
key_short,
activation_limit,
instances_count,
disabled,
status_formatted,
expires_at,
created_at,
updated_at,
test_mode,
];
for (const item of items) expect(item).toBeDefined();
expect(Object.keys(attributes).length).toEqual(items.length);
expect(store_id).toEqual(Number(storeId));
expect(order_id).toEqual(Number(orderId));
expect(order_item_id).toEqual(Number(orderItemId));
expect(product_id).toEqual(Number(productId));
expect(expires_at).toBeNull();

const {
store,
customer,
order,
"order-item": orderItem,
product,
"license-key-instances": licenseKeyInstances,
} = relationships;
const relationshipItems = [
store,
customer,
order,
orderItem,
product,
licenseKeyInstances,
];
for (const item of relationshipItems) expect(item).toBeDefined();
expect(Object.keys(relationships).length).toEqual(relationshipItems.length);
});

it("Should return a license key object with expires_at is 2024-05-26", async () => {
const date = new Date(new Date().getTime() + 86400000)
.toISOString()
.split("T")[0];
const expiresAt = `${date}T00:00:00.000000Z`;
const {
statusCode,
error,
data: _data,
} = await updateLicenseKey(licenseKeyId, {
expiresAt,
});
expect(statusCode).toEqual(200);
expect(error).toBeNull();
expect(_data).toBeDefined();

const { data, links } = _data!;
expect(links.self).toEqual(`${API_BASE_URL}${PATH}${licenseKeyId}`);

const { type, id, attributes, relationships } = data;
expect(type).toEqual(DATA_TYPE);
expect(id).toEqual(licenseKeyId.toString());
expect(attributes).toBeDefined();
expect(relationships).toBeDefined();

const {
store_id,
customer_id,
order_id,
order_item_id,
product_id,
status,
key,
user_name,
user_email,
key_short,
activation_limit,
instances_count,
disabled,
status_formatted,
expires_at,
created_at,
updated_at,
test_mode,
} = attributes;
const items = [
store_id,
customer_id,
order_id,
order_item_id,
product_id,
status,
key,
user_name,
user_email,
key_short,
activation_limit,
instances_count,
disabled,
status_formatted,
expires_at,
created_at,
updated_at,
test_mode,
];
for (const item of items) expect(item).toBeDefined();
expect(Object.keys(attributes).length).toEqual(items.length);
expect(store_id).toEqual(Number(storeId));
expect(order_id).toEqual(Number(orderId));
expect(order_item_id).toEqual(Number(orderItemId));
expect(product_id).toEqual(Number(productId));
expect(expires_at).toEqual(expiresAt);

const {
store,
customer,
order,
"order-item": orderItem,
product,
"license-key-instances": licenseKeyInstances,
} = relationships;
const relationshipItems = [
store,
customer,
order,
orderItem,
product,
licenseKeyInstances,
];
for (const item of relationshipItems) expect(item).toBeDefined();
expect(Object.keys(relationships).length).toEqual(relationshipItems.length);
});

it("Should return a license key object with disabled is true", async () => {
const {
statusCode,
Expand Down

0 comments on commit 218d2ef

Please sign in to comment.