Skip to content

Commit 4b8944f

Browse files
kvzremcohaszing
andauthored
Sync alphalib 2025 05 06 (#226)
* w * w * Update template.ts * Fix deep types error (#227) * Fix deep types error One of the big features of TypeScript, is that it can infer types based on context. This is powerful, but it comes at a cost. Determining types based on inference consumes much more resources than using a type annotation. This is an important reason to add explicit type annotations. We heavily rely on Zod though. Zod is powerful, but it heavily relies on inference. This means that by using Zod, we sacrifice type checking performance. Zod 4 will supposedly be more performant. This is great, but by definition it can’t be as performant as regular types. When the types become really complex, TypeScript may error on this. An explicit type annotation in the right spot fixes this. This PR uses this to resolve the type error. I can’t explain why this surfaced here, but not in our internal repos these schemas originate from. * Update src/alphalib/types/template.ts --------- Co-authored-by: Kevin van Zonneveld <vanzonneveld@gmail.com> --------- Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
1 parent 4c7b7e3 commit 4b8944f

File tree

97 files changed

+2283
-724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+2283
-724
lines changed

examples/convert_to_webp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ const status = await transloadit.createAssembly({
3232
},
3333
waitForCompletion: true,
3434
})
35-
console.log('Your WebP file:', status.results.webp[0].url)
35+
console.log('Your WebP file:', status.results?.webp?.[0]?.url)

examples/face_detect_download.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const status = await transloadit.createAssembly({
4444
// Now save the file
4545
const outPath = './output-face.jpg'
4646
const stream = createWriteStream(outPath)
47-
const { url } = status.results.facesDetected[0]
47+
const url = status.results?.facesDetected?.[0]?.url
4848
assert(url != null)
49-
await got.stream(url).pipe(stream)
49+
got.stream(url).pipe(stream)
5050
console.log('Your cropped face has been saved to', outPath)

examples/rasterize_svg_to_png.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ const status = await transloadit.createAssembly({
3030
},
3131
waitForCompletion: true,
3232
})
33-
console.log('Your PNG file:', status.results.png[0].url)
33+
console.log('Your PNG file:', status.results?.png?.[0]?.url)

examples/resize_an_image.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ const status = await transloadit.createAssembly({
3333
},
3434
waitForCompletion: true,
3535
})
36-
console.log('Your resized image:', status.results.resize[0].url)
36+
console.log('Your resized image:', status.results?.resize?.[0]?.url)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"is-stream": "^4.0.1",
2727
"p-map": "^7.0.3",
2828
"tus-js-client": "^4.3.1",
29-
"type-fest": "^4.39.1",
29+
"type-fest": "^4.41.0",
3030
"zod": "^3.24.2"
3131
},
3232
"devDependencies": {
@@ -37,7 +37,7 @@
3737
"@types/temp": "^0.9.4",
3838
"@typescript-eslint/eslint-plugin": "^8.29.1",
3939
"@typescript-eslint/parser": "^8.29.1",
40-
"@vitest/coverage-v8": "^3.1.1",
40+
"@vitest/coverage-v8": "^3.1.3",
4141
"badge-maker": "^4.1.0",
4242
"eslint": "8",
4343
"eslint-config-prettier": "^8.10.0",
@@ -56,7 +56,7 @@
5656
"prettier": "^3.5.3",
5757
"temp": "^0.9.4",
5858
"typescript": "^5.8.3",
59-
"vitest": "^3.1.1"
59+
"vitest": "^3.1.3"
6060
},
6161
"repository": {
6262
"type": "git",

src/Transloadit.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ export class Transloadit {
310310
}
311311

312312
if (!waitForCompletion) return result
313+
314+
if (result.assembly_id == null) {
315+
throw new InconsistentResponseError(
316+
'Server returned an assembly response without an assembly_id after creation'
317+
)
318+
}
313319
const awaitResult = await this.awaitAssemblyCompletion(result.assembly_id, {
314320
timeout,
315321
onAssemblyProgress,
@@ -341,10 +347,18 @@ export class Transloadit {
341347
while (true) {
342348
const result = await this.getAssembly(assemblyId)
343349

350+
// If 'ok' is not in result, it implies a terminal state (e.g., error, completed, canceled).
351+
// If 'ok' is present, then we check if it's one of the non-terminal polling states.
344352
if (
345-
result.ok !== 'ASSEMBLY_UPLOADING' &&
346-
result.ok !== 'ASSEMBLY_EXECUTING' &&
347-
result.ok !== 'ASSEMBLY_REPLAYING'
353+
!('ok' in result) ||
354+
(result.ok !== 'ASSEMBLY_UPLOADING' &&
355+
result.ok !== 'ASSEMBLY_EXECUTING' &&
356+
// ASSEMBLY_REPLAYING is not a valid 'ok' status for polling, it means it's done replaying.
357+
// The API does not seem to have an ASSEMBLY_REPLAYING status in the typical polling loop.
358+
// It's usually a final status from the replay endpoint.
359+
// For polling, we only care about UPLOADING and EXECUTING.
360+
// If a replay operation puts it into a pollable state, that state would be EXECUTING.
361+
result.ok !== 'ASSEMBLY_REPLAYING') // This line might need review based on actual API behavior for replayed assembly polling
348362
) {
349363
return result // Done!
350364
}

0 commit comments

Comments
 (0)