diff --git a/lib/dockerfile/index.ts b/lib/dockerfile/index.ts index db48000..7063c1a 100644 --- a/lib/dockerfile/index.ts +++ b/lib/dockerfile/index.ts @@ -33,7 +33,7 @@ export function process( return line[0] !== '#' ? lodash.template(line, { interpolate: /%%([A-Z][A-Z_]+)%%/ })( variables, - ) + ) : line; }) .join('\n'); diff --git a/lib/release/api.ts b/lib/release/api.ts index 381cf30..161fd24 100644 --- a/lib/release/api.ts +++ b/lib/release/api.ts @@ -11,7 +11,7 @@ const MAX_CONCURRENT_REQUESTS = 5; export interface ClientConfig { /** * The host address of the API server to use, complete with the protocol, - * eg. `https://api.balena-cloud.com`. This module will issue requests to v4 of + * eg. `https://api.balena-cloud.com`. This module will issue requests to v6 of * the API. */ apiEndpoint: string; @@ -177,7 +177,13 @@ export async function updateRelease( id: number, body: Partial, ): Promise { - return models.update(api, 'release', id, body); + await api + .patch({ + resource: 'release', + id, + body, + } as const) + .catch(models.wrapResponseError); } export async function updateImage( @@ -185,7 +191,13 @@ export async function updateImage( id: number, body: Partial, ): Promise { - return models.update(api, 'image', id, body); + await api + .patch({ + resource: 'image', + id, + body, + } as const) + .catch(models.wrapResponseError); } // Helpers @@ -194,31 +206,62 @@ async function getUser( api: PinejsClientCore, id: number, ): Promise { - return models.get(api, 'user', id); + const user = await api + .get({ + resource: 'user', + id, + options: { $select: 'id' }, + } as const) + .catch(models.wrapResponseError); + if (user == null) { + throw new Error('Could not find user with id: ' + id); + } + return user; } async function getApplication( api: PinejsClientCore, id: number, ): Promise { - return models.get(api, 'application', id); + const app = await api + .get({ + resource: 'application', + id, + options: { $select: 'id' }, + } as const) + .catch(models.wrapResponseError); + if (app == null) { + throw new Error('Could not find application with id: ' + id); + } + return app; } async function getOrCreateService( api: PinejsClientCore, body: models.ServiceAttributes, ): Promise { - return models.getOrCreate(api, 'service', body, { - application: body.application, - service_name: body.service_name, - }); + return (await api + .getOrCreate({ + resource: 'service', + id: { + application: body.application, + service_name: body.service_name, + }, + body, + } as const) + .catch(models.wrapResponseError)) as models.ServiceModel; } async function createRelease( api: PinejsClientCore, body: models.ReleaseAttributes, ): Promise { - return models.create(api, 'release', body); + return (await api + .post({ + resource: 'release', + body, + }) + .catch(models.wrapResponseError)) as models.ReleaseModel; } async function createImage( @@ -228,29 +271,37 @@ async function createImage( envvars: Dict | undefined, body: models.ImageAttributes, ): Promise { - const image = await models.create( - api, - 'image', - body, - ); - - const releaseImage = await models.create< - models.ReleaseImageModel, - models.ReleaseImageAttributes - >(api, 'image__is_part_of__release', { - is_part_of__release: release, - image: image.id, - }); + const image = (await api + .post({ + resource: 'image', + body, + } as const) + .catch(models.wrapResponseError)) as models.ImageModel; + + const releaseImage = (await api + .post({ + resource: 'image__is_part_of__release', + body: { + is_part_of__release: release, + image: image.id, + }, + } as const) + .catch(models.wrapResponseError)) as models.ReleaseImageModel; if (labels) { await pMap( Object.entries(labels), - ([name, value]) => { - return models.create(api, 'image_label', { - release_image: releaseImage.id, - label_name: name, - value: (value || '').toString(), - }); + async ([name, value]) => { + await api + .post({ + resource: 'image_label', + body: { + release_image: releaseImage.id, + label_name: name, + value: (value || '').toString(), + }, + } as const) + .catch(models.wrapResponseError); }, { concurrency: MAX_CONCURRENT_REQUESTS, @@ -261,12 +312,17 @@ async function createImage( if (envvars) { await pMap( Object.entries(envvars), - ([name, value]) => { - return models.create(api, 'image_environment_variable', { - release_image: releaseImage.id, - name, - value: (value || '').toString(), - }); + async ([name, value]) => { + await api + .post({ + resource: 'image_environment_variable', + body: { + release_image: releaseImage.id, + name, + value: (value || '').toString(), + }, + } as const) + .catch(models.wrapResponseError); }, { concurrency: MAX_CONCURRENT_REQUESTS, diff --git a/lib/release/models.ts b/lib/release/models.ts index 37ea1cb..0bc1de6 100644 --- a/lib/release/models.ts +++ b/lib/release/models.ts @@ -1,10 +1,3 @@ -import type { - Expand, - Filter, - ODataOptions, - PinejsClientCore, -} from 'pinejs-client-core'; - import type { Composition } from '../../lib/parse'; import * as errors from './errors'; @@ -99,69 +92,8 @@ export interface ReleaseImageModel extends ReleaseImageAttributesBase { // Helpers -export function getOrCreate( - api: PinejsClientCore, - resource: string, - body: U, - filter: V, -): Promise { - return create(api, resource, body).catch((error) => { - if (error instanceof errors.UniqueConstraintError) { - return find(api, resource, { $filter: filter }).then((obj) => { - if (obj.length > 0) { - return obj[0]; - } - throw new errors.ObjectDoesNotExistError(); - }); - } - throw error; - }) as Promise; -} - -export function create( - api: PinejsClientCore, - resource: string, - body: U, -): Promise { - return api.post({ resource, body }).catch(wrapResponseError) as Promise; -} - -export function update( - api: PinejsClientCore, - resource: string, - id: number, - body: T, -): Promise { - return api.patch({ resource, id, body }).catch(wrapResponseError); -} - -export function find( - api: PinejsClientCore, - resource: string, - options: ODataOptions, -): Promise { - return api.get({ resource, options }).catch(wrapResponseError) as Promise< - T[] - >; -} - -export function get( - api: PinejsClientCore, - resource: string, - id: number, - expand?: Expand, -): Promise { - return api - .get({ - resource, - id, - options: expand ? { $expand: expand } : undefined, - }) - .catch(wrapResponseError) as Promise; -} - -function wrapResponseError(e: E): void { - const error: { statusCode?: number; message?: unknown } = e as any; +export function wrapResponseError(e: Error): void { + const error: { statusCode?: number; message?: unknown } = e; if (!error.statusCode) { throw e; } diff --git a/package.json b/package.json index 86cb659..be10db0 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "proxyquire": "^2.1.3", "rimraf": "^5.0.1", "ts-mocha": "^10.0.0", - "typescript": "^5.2.2" + "typescript": "^5.5.3" }, "dependencies": { "ajv": "^6.12.3", @@ -83,8 +83,8 @@ "memoizee": "^0.4.15", "mz": "^2.7.0", "p-map": "^4.0.0", - "pinejs-client-core": "^6.13.0", - "pinejs-client-request": "^7.3.5", + "pinejs-client-core": "^6.15.10", + "pinejs-client-request": "^7.5.2", "request": "^2.88.2", "semver": "^7.3.5", "stream-to-promise": "^3.0.0",