@kabobcompany/lift 0.1.0 → 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
@@ -13,6 +13,9 @@ const { devices } = await lookr(session, "GET", "devices");
13
13
  Kabob. `LIFT_ISSUER`, `LIFT_CLIENT_ID` and `LIFT_CLIENT_SECRET` are read from the
14
14
  environment, where Lift puts them.
15
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
+
16
19
  A refusal is thrown as a `LiftError` with `status`, `code`, `message` and, on a 422,
17
20
  `issues`.
18
21
 
package/dist/index.d.ts CHANGED
@@ -26,6 +26,8 @@ export type Options = {
26
26
  companyId?: number | null;
27
27
  query?: Record<string, string | string[]>;
28
28
  body?: unknown;
29
+ /** How long to wait for the answer. Default: 25 seconds. */
30
+ timeoutMs?: number;
29
31
  };
30
32
  /**
31
33
  * Call Lookr through Lift as the signed-in person:
@@ -37,6 +39,43 @@ export type Options = {
37
39
  * "Publish to screens" too. Lookr applies the person's own permissions on top.
38
40
  */
39
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>;
40
79
  export type Action = {
41
80
  id: string;
42
81
  name: string;
package/dist/index.js CHANGED
@@ -72,12 +72,40 @@ export async function lookr(who, method, path, options = {}) {
72
72
  ...(options.body !== undefined && { "content-type": "application/json" }),
73
73
  },
74
74
  body: options.body !== undefined ? JSON.stringify(options.body) : undefined,
75
- signal: AbortSignal.timeout(25_000),
75
+ signal: AbortSignal.timeout(options.timeoutMs ?? 25_000),
76
76
  });
77
77
  if (response.status === 401)
78
78
  throw new LiftError(401, "sign_in_again", "Sign in again to keep working with Kabob data.");
79
79
  return answer(response, "Lookr");
80
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
+ }
81
109
  /** Lift asks Lookr, as this person, whether they may interrupt those screens with that program, then keeps the action. */
82
110
  export const setUpAction = (who, input, companyId) => lookr(who, "POST", "actions", { body: input, companyId });
83
111
  /** The actions of the signed-in person's companies. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kabobcompany/lift",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, and run public actions.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",