Skip to content

Commit

Permalink
Allow injecting any PinejsClientCore compatible API client
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
thgreasi committed Nov 10, 2023
1 parent ec7b510 commit e0ab3ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
17 changes: 9 additions & 8 deletions lib/release/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pMap = require('p-map');
import type { PinejsClientCore } from 'pinejs-client-core';
import { PinejsClientRequest } from 'pinejs-client-request';
import * as models from './models';
import { Dict } from './types';
Expand Down Expand Up @@ -54,7 +55,7 @@ export interface Request {
* You can use the `createClient` convenience function of this module to create
* a client that can reused across requests.
*/
client: PinejsClientRequest;
client: PinejsClientCore<unknown>;

/**
* The ID of the user the release should belong to. The user authenticated via `client`
Expand Down Expand Up @@ -169,15 +170,15 @@ export async function create(req: Request): Promise<Response> {
}

export async function updateRelease(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
id: number,
body: Partial<models.ReleaseAttributes>,
): Promise<void> {
return models.update(api, 'release', id, body);
}

export async function updateImage(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
id: number,
body: Partial<models.ImageAttributes>,
): Promise<void> {
Expand All @@ -187,21 +188,21 @@ export async function updateImage(
// Helpers

async function getUser(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
id: number,
): Promise<models.UserModel> {
return models.get(api, 'user', id);
}

async function getApplication(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
id: number,
): Promise<models.ApplicationModel> {
return models.get(api, 'application', id);
}

async function getOrCreateService(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
body: models.ServiceAttributes,
): Promise<models.ServiceModel> {
return models.getOrCreate(api, 'service', body, {
Expand All @@ -211,14 +212,14 @@ async function getOrCreateService(
}

async function createRelease(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
body: models.ReleaseAttributes,
): Promise<models.ReleaseModel> {
return models.create(api, 'release', body);
}

async function createImage(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
release: number,
labels: Dict<string> | undefined,
envvars: Dict<string> | undefined,
Expand Down
18 changes: 11 additions & 7 deletions lib/release/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { PinejsClientRequest } from 'pinejs-client-request';
import type { Expand, Filter, ODataOptions } from 'pinejs-client-core';
import type {
Expand,
Filter,
ODataOptions,
PinejsClientCore,
} from 'pinejs-client-core';

import type { Composition } from '../../lib/parse';

Expand Down Expand Up @@ -96,7 +100,7 @@ export interface ReleaseImageModel extends ReleaseImageAttributesBase {
// Helpers

export function getOrCreate<T, U extends object, V extends Filter>(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
resource: string,
body: U,
filter: V,
Expand All @@ -115,15 +119,15 @@ export function getOrCreate<T, U extends object, V extends Filter>(
}

export function create<T, U extends object>(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
resource: string,
body: U,
): Promise<T> {
return api.post({ resource, body }).catch(wrapResponseError) as Promise<T>;
}

export function update<T extends object>(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
resource: string,
id: number,
body: T,
Expand All @@ -132,7 +136,7 @@ export function update<T extends object>(
}

export function find<T>(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
resource: string,
options: ODataOptions,
): Promise<T[]> {
Expand All @@ -142,7 +146,7 @@ export function find<T>(
}

export function get<T>(
api: PinejsClientRequest,
api: PinejsClientCore<unknown>,
resource: string,
id: number,
expand?: Expand,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"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",
"request": "^2.88.2",
"semver": "^7.3.5",
Expand Down

0 comments on commit e0ab3ef

Please sign in to comment.