Skip to content

Commit

Permalink
Merge pull request #1749 from balena-io/device-type-helper-renames
Browse files Browse the repository at this point in the history
deviceTypes: Rename and deprecate some helpers for clarify
  • Loading branch information
thgreasi authored Sep 10, 2024
2 parents 57f8dde + 71e6689 commit c59227f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function onInitHooks() {
_.flatMap(auth.KEYS, 'permissions'),
);
const { setSyncSettings } = await import('./src/features/contracts/index.js');
const { getAccessibleDeviceTypes } = await import(
const { getAccessibleDeviceTypeJsons } = await import(
'./src/features/device-types/device-types.js'
);

Expand Down Expand Up @@ -131,7 +131,7 @@ async function onInitHooks() {
});

// Pre-fetch the device types and populate the cache w/o blocking the API startup
void getAccessibleDeviceTypes(sbvrUtils.api.resin);
void getAccessibleDeviceTypeJsons(sbvrUtils.api.resin);

await sbvrUtils.db.transaction((tx) =>
createAll(tx, permissionNames, auth.ROLES, auth.KEYS, {}),
Expand Down
4 changes: 2 additions & 2 deletions src/features/device-config/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../../infra/error-handling/index.js';

import { generateConfig } from './device-config.js';
import { findBySlug } from '../device-types/device-types.js';
import { getDeviceTypeJsonBySlug } from '../device-types/device-types.js';
import { checkInt, getBodyOrQueryParam } from '../../lib/utils.js';

const { UnauthorizedError, NotFoundError } = errors;
Expand Down Expand Up @@ -58,7 +58,7 @@ export const downloadImageConfig: RequestHandler = async (req, res) => {
const resinApi = api.resin.clone({ passthrough: { req } });

const app = await getApp(appId, req);
const deviceTypeJson = await findBySlug(
const deviceTypeJson = await getDeviceTypeJsonBySlug(
resinApi,
deviceTypeSlug || app.is_for__device_type[0].slug,
);
Expand Down
6 changes: 3 additions & 3 deletions src/features/device-types/device-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export const validateSlug = (slug?: string) => {
return slug;
};

/** @deprecated */
export const getAccessibleDeviceTypes = async (
/** @deprecated Prefer querying the device_type resource directly unless you need the device-type.json contents. */
export const getAccessibleDeviceTypeJsons = async (
resinApi: typeof sbvrUtils.api.resin,
): Promise<DeviceTypeJson[]> => {
const [deviceTypeInfosBySlug, accessibleDeviceTypes] = await Promise.all([
Expand All @@ -101,7 +101,7 @@ export const getAccessibleDeviceTypes = async (
};

/** @deprecated Use the getDeviceTypeBySlug unless you need the device-type.json contents. */
export const findBySlug = async (
export const getDeviceTypeJsonBySlug = async (
resinApi: typeof sbvrUtils.api.resin,
slug: string,
): Promise<DeviceTypeJson> =>
Expand Down
5 changes: 3 additions & 2 deletions src/features/device-types/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const { api } = sbvrUtils;
export const getDeviceTypes: RequestHandler = async (req, res) => {
const resinApi = api.resin.clone({ passthrough: { req } });
try {
const deviceTypes = await deviceTypesLib.getAccessibleDeviceTypes(resinApi);
const deviceTypes =
await deviceTypesLib.getAccessibleDeviceTypeJsons(resinApi);
res.json(deviceTypes);
} catch (err) {
captureException(err, 'Error getting device types', { req });
Expand All @@ -31,7 +32,7 @@ export const getDeviceType: RequestHandler = async (req, res) => {
try {
const resinApi = api.resin.clone({ passthrough: { req } });
const slug = deviceTypesLib.validateSlug(req.params.deviceType);
const data = await deviceTypesLib.findBySlug(resinApi, slug);
const data = await deviceTypesLib.getDeviceTypeJsonBySlug(resinApi, slug);
res.json(data);
} catch (err) {
captureException(err, 'Error getting device type', {
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ import {
getUserIDFromCreds,
} from './infra/rate-limiting/index.js';
import {
getAccessibleDeviceTypes,
findBySlug,
getAccessibleDeviceTypeJsons,
getDeviceTypeJsonBySlug,
getDeviceTypeBySlug,
} from './features/device-types/device-types.js';
import { proxy as supervisorProxy } from './features/device-proxy/device-proxy.js';
Expand Down Expand Up @@ -247,8 +247,14 @@ export const release = {
addVirtualFieldsToModel: addReleaseAdditionsToModel,
};
export const deviceTypes = {
getAccessibleDeviceTypes,
findBySlug,
// TODO: Drop me in the next major
/** @deprecated Use getAccessibleDeviceTypeJsons */
getAccessibleDeviceTypes: getAccessibleDeviceTypeJsons,
getAccessibleDeviceTypeJsons,
// TODO: Drop me in the next major
/** @deprecated Use getDeviceTypeJsonBySlug */
findBySlug: getDeviceTypeJsonBySlug,
getDeviceTypeJsonBySlug,
getDeviceTypeBySlug,
};
export * as contracts from './exports/contracts.js';
Expand Down

0 comments on commit c59227f

Please sign in to comment.