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

Add $count option to get api keys methods #1293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
93 changes: 59 additions & 34 deletions lib/models/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,30 @@ const getApiKeysModel = function (
*/
async getAllNamedUserApiKeys(
options: BalenaSdk.PineOptions<BalenaSdk.ApiKey> = {},
): Promise<BalenaSdk.ApiKey[]> {
return await exports.getAll(
mergePineOptions(
{
$filter: {
is_of__actor: await sdkInstance.auth.getUserActorId(),
// the only way to reason whether it's
// a named user api key vs a deprecated user api key
// is whether it has a name.
name: {
$ne: null,
},
): Promise<BalenaSdk.ApiKey[] | number> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to keep this in the UI atm, otherwise in order to get it in the SDK we need to define overloads for the methods, so that when TS sees that $count is defined it then uses the overload that returns a number.
You can check the .get() pinejs-client typings in the SDK to check how we defined the overloads for when ParamsObjWithCount is provided.

const internalOptions = {
$filter: {
is_of__actor: await sdkInstance.auth.getUserActorId(),
// the only way to reason whether it's
// a named user api key vs a deprecated user api key
// is whether it has a name.
name: {
$ne: null,
},
},
};
if (options.$count) {
return await pine.get({
resource: 'api_key',
options: {
$count: {
...internalOptions,
...options.$count,
},
},
options,
),
);
});
}
return await exports.getAll(mergePineOptions(internalOptions, options));
},

/**
Expand Down Expand Up @@ -210,21 +217,30 @@ const getApiKeysModel = function (
async getProvisioningApiKeysByApplication(
slugOrUuidOrId: string | number,
options: BalenaSdk.PineOptions<BalenaSdk.ApiKey> = {},
): Promise<BalenaSdk.ApiKey[]> {
): Promise<BalenaSdk.ApiKey[] | number> {
const { actor } = await applicationModel().get(slugOrUuidOrId, {
$select: 'actor',
});

return await exports.getAll(
mergePineOptions(
{
$filter: {
is_of__actor: actor,
const internalOptions = {
$filter: {
is_of__actor: actor,
},
};

if (options.$count) {
return await pine.get({
resource: 'api_key',
options: {
$count: {
...internalOptions,
...options.$count,
},
},
options,
),
);
});
}

return await exports.getAll(mergePineOptions(internalOptions, options));
},

/**
Expand Down Expand Up @@ -253,22 +269,31 @@ const getApiKeysModel = function (
async getDeviceApiKeysByDevice(
uuidOrId: string | number,
options: BalenaSdk.PineOptions<BalenaSdk.ApiKey> = {},
): Promise<BalenaSdk.ApiKey[]> {
): Promise<BalenaSdk.ApiKey[] | number> {
const { actor } = await deviceModel().get(uuidOrId, {
$select: 'actor',
});
const internalOptions = {
$filter: {
is_of__actor: actor,
},
};

return await pine.get({
resource: 'api_key',
options: mergePineOptions(
{
$filter: {
is_of__actor: actor,
if (options.$count) {
return await pine.get({
resource: 'api_key',
options: {
$count: {
...internalOptions,
...options.$count,
},
Comment on lines +286 to 289
Copy link
Member

@thgreasi thgreasi Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be v ?

Suggested change
$count: {
...internalOptions,
...options.$count,
},
$count: mergePineOptions(internalOptions, options.$count),

Applies in all methods

$orderby: 'name asc',
},
options,
),
});
}

return await pine.get({
resource: 'api_key',
options: mergePineOptions(internalOptions, options),
});
},

Expand Down