@kabobcompany/lift 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,28 @@
1
- # Temporary Holding Version
1
+ # @kabobcompany/lift
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ For apps hosted on Lift, Kabob's internal app platform: call Kabob products through Lift
4
+ as the person signed in to the app, and run public actions. Server side only.
5
+
6
+ ```ts
7
+ import { lookr, runAction, LiftError } from "@kabobcompany/lift";
8
+
9
+ const { devices } = await lookr(session, "GET", "devices");
10
+ ```
11
+
12
+ `session` is anything with `accessToken` and `accessTokenExpiresAt` from Sign in with
13
+ Kabob. `LIFT_ISSUER`, `LIFT_CLIENT_ID` and `LIFT_CLIENT_SECRET` are read from the
14
+ environment, where Lift puts them.
15
+
16
+ `importMedia(session, { url, name })` has Lookr download a file into the media library, and
17
+ `mediaReady(session, id)` waits until a video has finished transcoding.
18
+
19
+ A refusal is thrown as a `LiftError` with `status`, `code`, `message` and, on a 422,
20
+ `issues`.
21
+
22
+ Which calls exist, with their bodies and answers, is documented in each project's
23
+ `docs/lift/` (the kit Lift hands out), not here.
24
+
25
+ ## Releasing
26
+
27
+ `npm version <patch|minor>` then `npm publish` (it builds first). A new function here
28
+ goes with a new version of the kit's documentation in `templates/starter/docs/lift`.
@@ -0,0 +1,121 @@
1
+ /** Who is signed in, as far as Lift's integrations need to know. */
2
+ export type Signed = {
3
+ /** The Lift access token from Sign in with Kabob. */
4
+ accessToken?: string | null;
5
+ /** When it stops working, in seconds since 1970. */
6
+ accessTokenExpiresAt?: number | null;
7
+ };
8
+ /** A refusal from Lift or from the product behind it. */
9
+ export declare class LiftError extends Error {
10
+ status: number;
11
+ code: string;
12
+ /** The product's validation issues, on a 422: `{ level, code, message, path }`. Act on `code`. */
13
+ issues: unknown[];
14
+ constructor(status: number, code: string, message: string, issues?: unknown[]);
15
+ }
16
+ type Settings = {
17
+ issuer?: string;
18
+ clientId?: string;
19
+ clientSecret?: string;
20
+ };
21
+ /** Only needed outside Lift, or in tests: by default the LIFT_* environment variables are used. */
22
+ export declare function configure(settings: Settings): void;
23
+ export type Method = "GET" | "POST" | "PATCH" | "DELETE";
24
+ export type Options = {
25
+ /** Which of the person's companies to act in. Default: their current one. */
26
+ companyId?: number | null;
27
+ query?: Record<string, string | string[]>;
28
+ body?: unknown;
29
+ /** How long to wait for the answer. Default: 25 seconds. */
30
+ timeoutMs?: number;
31
+ };
32
+ /**
33
+ * Call Lookr through Lift as the signed-in person:
34
+ *
35
+ * await lookr(session, "GET", "devices")
36
+ * await lookr(session, "POST", "deployments", { body: { program_id: 12, device_ids: [5] } })
37
+ *
38
+ * Reading needs the app's Lookr switch on Lift; changing anything needs
39
+ * "Publish to screens" too. Lookr applies the person's own permissions on top.
40
+ */
41
+ export declare function lookr<T = unknown>(who: Signed, method: Method, path: string, options?: Options): Promise<T>;
42
+ export type Media = {
43
+ id: number;
44
+ name: string;
45
+ kind: "video" | "image" | "audio" | "pdf" | "other";
46
+ /** False while a video is still transcoding: it can't be deployed yet. */
47
+ ready: boolean;
48
+ process_status?: string;
49
+ width?: number;
50
+ height?: number;
51
+ seconds?: number;
52
+ bytes?: number;
53
+ thumb_url?: string;
54
+ folder_id?: number;
55
+ /** What a clip's asset takes: `lookr://resource/<id>`. */
56
+ uri: string;
57
+ };
58
+ /**
59
+ * Have Lookr download the file at `url` (public, http or https) into the
60
+ * company's media library. Needs "Publish to screens". The call waits while
61
+ * Lookr fetches and stores the file; a video comes back with `ready: false`
62
+ * (see `mediaReady`). If this times out the import may still have finished:
63
+ * look for the name with `GET media?q=` before trying again.
64
+ */
65
+ export declare const importMedia: (who: Signed, input: {
66
+ url: string;
67
+ name?: string;
68
+ }, companyId?: number | null) => Promise<Media>;
69
+ /**
70
+ * Wait until a media item can be played (a video transcodes after it is
71
+ * imported) and answer it. Throws a LiftError with code "media_not_ready"
72
+ * when it still isn't after `timeoutMs` (10 minutes unless said otherwise).
73
+ */
74
+ export declare function mediaReady(who: Signed, id: number, options?: {
75
+ companyId?: number | null;
76
+ timeoutMs?: number;
77
+ everyMs?: number;
78
+ }): Promise<Media>;
79
+ export type Action = {
80
+ id: string;
81
+ name: string;
82
+ company_id: number;
83
+ program_id: number;
84
+ program_name: string | null;
85
+ device_ids: number[];
86
+ seconds: number;
87
+ cooldown_seconds: number;
88
+ set_up_by: string | null;
89
+ last_run_at: string | null;
90
+ run_count: number;
91
+ };
92
+ export type ActionInput = {
93
+ name: string;
94
+ program_id: number;
95
+ device_ids: number[];
96
+ seconds: number;
97
+ cooldown_seconds?: number;
98
+ };
99
+ /** Lift asks Lookr, as this person, whether they may interrupt those screens with that program, then keeps the action. */
100
+ export declare const setUpAction: (who: Signed, input: ActionInput, companyId?: number | null) => Promise<Action>;
101
+ /** The actions of the signed-in person's companies. */
102
+ export declare const listActions: (who: Signed) => Promise<Action[]>;
103
+ export declare const removeAction: (who: Signed, id: string) => Promise<void>;
104
+ /** Every action set up in this app, whoever set it up. */
105
+ export declare const listAllActions: () => Promise<Action[]>;
106
+ /**
107
+ * Run an action for whoever asked. Nothing is sent but its id. Throws a
108
+ * LiftError with code "too_soon" (429) while its cooldown lasts, and with
109
+ * Lookr's own refusal when the person who set it up may no longer do this.
110
+ */
111
+ export declare const runAction: (id: string) => Promise<{
112
+ action_id: string;
113
+ deployment_id: number;
114
+ device_ids: number[];
115
+ ends_at: string;
116
+ }>;
117
+ /** End the action's last run before its time is up. */
118
+ export declare const stopAction: (id: string) => Promise<{
119
+ stopped_device_ids: number[];
120
+ }>;
121
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ // @kabobcompany/lift: what an app hosted on Lift uses to work with Kabob
2
+ // products through Lift. Server side only.
3
+ //
4
+ // Lift sets LIFT_ISSUER, LIFT_CLIENT_ID and LIFT_CLIENT_SECRET in the app's
5
+ // container; this package reads them, or what configure() was given.
6
+ /** A refusal from Lift or from the product behind it. */
7
+ export class LiftError extends Error {
8
+ status;
9
+ code;
10
+ /** The product's validation issues, on a 422: `{ level, code, message, path }`. Act on `code`. */
11
+ issues;
12
+ constructor(status, code, message, issues = []) {
13
+ super(message);
14
+ this.name = "LiftError";
15
+ this.status = status;
16
+ this.code = code;
17
+ this.issues = issues;
18
+ }
19
+ }
20
+ let given = {};
21
+ /** Only needed outside Lift, or in tests: by default the LIFT_* environment variables are used. */
22
+ export function configure(settings) {
23
+ given = { ...given, ...settings };
24
+ }
25
+ function settings() {
26
+ const issuer = (given.issuer ?? process.env.LIFT_ISSUER)?.replace(/\/$/, "");
27
+ if (!issuer) {
28
+ throw new LiftError(503, "not_configured", "Lift integrations need LIFT_ISSUER, which is set when the app runs on Lift.");
29
+ }
30
+ return {
31
+ issuer,
32
+ clientId: given.clientId ?? process.env.LIFT_CLIENT_ID,
33
+ clientSecret: given.clientSecret ?? process.env.LIFT_CLIENT_SECRET,
34
+ };
35
+ }
36
+ async function answer(response, product) {
37
+ if (response.status === 204)
38
+ return undefined;
39
+ const body = (await response.json().catch(() => null));
40
+ if (!response.ok) {
41
+ const issues = Array.isArray(body?.issues) ? body.issues : [];
42
+ throw new LiftError(response.status, String(body?.error ?? "integration_error"), String(body?.message ?? `${product} answered ${response.status}.`), issues);
43
+ }
44
+ return body;
45
+ }
46
+ /**
47
+ * Call Lookr through Lift as the signed-in person:
48
+ *
49
+ * await lookr(session, "GET", "devices")
50
+ * await lookr(session, "POST", "deployments", { body: { program_id: 12, device_ids: [5] } })
51
+ *
52
+ * Reading needs the app's Lookr switch on Lift; changing anything needs
53
+ * "Publish to screens" too. Lookr applies the person's own permissions on top.
54
+ */
55
+ export async function lookr(who, method, path, options = {}) {
56
+ const { issuer } = settings();
57
+ // Lift issues no refresh tokens: after an hour the person signs in again.
58
+ if (!who.accessToken || (who.accessTokenExpiresAt ?? 0) <= Date.now() / 1000) {
59
+ throw new LiftError(401, "sign_in_again", "Sign in again to keep working with Kabob data.");
60
+ }
61
+ const url = new URL(`${issuer}/integrations/lookr/${path.replace(/^\//, "")}`);
62
+ if (options.companyId != null)
63
+ url.searchParams.set("company_id", String(options.companyId));
64
+ for (const [key, value] of Object.entries(options.query ?? {})) {
65
+ for (const item of Array.isArray(value) ? value : [value])
66
+ url.searchParams.append(key, item);
67
+ }
68
+ const response = await fetch(url, {
69
+ method,
70
+ headers: {
71
+ authorization: `Bearer ${who.accessToken}`,
72
+ ...(options.body !== undefined && { "content-type": "application/json" }),
73
+ },
74
+ body: options.body !== undefined ? JSON.stringify(options.body) : undefined,
75
+ signal: AbortSignal.timeout(options.timeoutMs ?? 25_000),
76
+ });
77
+ if (response.status === 401)
78
+ throw new LiftError(401, "sign_in_again", "Sign in again to keep working with Kabob data.");
79
+ return answer(response, "Lookr");
80
+ }
81
+ /**
82
+ * Have Lookr download the file at `url` (public, http or https) into the
83
+ * company's media library. Needs "Publish to screens". The call waits while
84
+ * Lookr fetches and stores the file; a video comes back with `ready: false`
85
+ * (see `mediaReady`). If this times out the import may still have finished:
86
+ * look for the name with `GET media?q=` before trying again.
87
+ */
88
+ export const importMedia = (who, input, companyId) => lookr(who, "POST", "media", { body: input, companyId, timeoutMs: 60_000 });
89
+ /**
90
+ * Wait until a media item can be played (a video transcodes after it is
91
+ * imported) and answer it. Throws a LiftError with code "media_not_ready"
92
+ * when it still isn't after `timeoutMs` (10 minutes unless said otherwise).
93
+ */
94
+ export async function mediaReady(who, id, options = {}) {
95
+ const until = Date.now() + (options.timeoutMs ?? 600_000);
96
+ for (;;) {
97
+ const media = await lookr(who, "GET", `media/${id}`, { companyId: options.companyId });
98
+ if (media.ready)
99
+ return media;
100
+ if (media.process_status === "failed") {
101
+ throw new LiftError(422, "media_failed", "Lookr could not process this file. Try another file.");
102
+ }
103
+ if (Date.now() >= until) {
104
+ throw new LiftError(504, "media_not_ready", "Lookr is still preparing this file. Try again in a few minutes.");
105
+ }
106
+ await new Promise((resolve) => setTimeout(resolve, options.everyMs ?? 5_000));
107
+ }
108
+ }
109
+ /** Lift asks Lookr, as this person, whether they may interrupt those screens with that program, then keeps the action. */
110
+ export const setUpAction = (who, input, companyId) => lookr(who, "POST", "actions", { body: input, companyId });
111
+ /** The actions of the signed-in person's companies. */
112
+ export const listActions = async (who) => (await lookr(who, "GET", "actions")).actions;
113
+ export const removeAction = (who, id) => lookr(who, "DELETE", `actions/${encodeURIComponent(id)}`);
114
+ // With nobody signed in the app speaks for itself, with its client secret.
115
+ async function asApp(method, path) {
116
+ const { issuer, clientId, clientSecret } = settings();
117
+ if (!clientId || !clientSecret) {
118
+ throw new LiftError(503, "not_configured", "Public actions need LIFT_CLIENT_ID and LIFT_CLIENT_SECRET.");
119
+ }
120
+ const basic = Buffer.from(`${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret)}`).toString("base64");
121
+ const response = await fetch(`${issuer}/integrations/lookr/${path}`, {
122
+ method,
123
+ headers: { authorization: `Basic ${basic}` },
124
+ signal: AbortSignal.timeout(25_000),
125
+ });
126
+ return answer(response, "Lift");
127
+ }
128
+ /** Every action set up in this app, whoever set it up. */
129
+ export const listAllActions = async () => (await asApp("GET", "actions")).actions;
130
+ /**
131
+ * Run an action for whoever asked. Nothing is sent but its id. Throws a
132
+ * LiftError with code "too_soon" (429) while its cooldown lasts, and with
133
+ * Lookr's own refusal when the person who set it up may no longer do this.
134
+ */
135
+ export const runAction = (id) => asApp("POST", `actions/${encodeURIComponent(id)}/run`);
136
+ /** End the action's last run before its time is up. */
137
+ export const stopAction = (id) => asApp("POST", `actions/${encodeURIComponent(id)}/stop`);
package/package.json CHANGED
@@ -1,6 +1,21 @@
1
1
  {
2
2
  "name": "@kabobcompany/lift",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, and run public actions.",
5
+ "license": "UNLICENSED",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }
11
+ },
12
+ "files": ["dist", "README.md"],
13
+ "engines": { "node": ">=20" },
14
+ "scripts": {
15
+ "build": "tsc -p .",
16
+ "prepublishOnly": "npm run build"
17
+ },
18
+ "publishConfig": { "access": "public" },
19
+ "repository": { "type": "git", "url": "git+https://github.com/kabobcompany/lift.git", "directory": "packages/lift" },
20
+ "devDependencies": { "typescript": "^5.9.0", "@types/node": "^24.0.0" }
21
+ }