Skip to content

Commit 87d9c3f

Browse files
committed
fix onedrive
1 parent 7c6463d commit 87d9c3f

3 files changed

Lines changed: 66 additions & 42 deletions

File tree

apps/sim/app/api/tools/onedrive/upload/route.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ import { createLogger } from '@/lib/logs/console/logger'
66
import { processSingleFileToUserFile } from '@/lib/uploads/utils/file-utils'
77
import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server'
88
import { generateRequestId } from '@/lib/utils'
9+
import { normalizeExcelValues } from '@/tools/onedrive/utils'
910

1011
export const dynamic = 'force-dynamic'
1112

1213
const logger = createLogger('OneDriveUploadAPI')
1314

1415
const MICROSOFT_GRAPH_BASE = 'https://graph.microsoft.com/v1.0'
1516

17+
const ExcelCellSchema = z.union([z.string(), z.number(), z.boolean(), z.null()])
18+
const ExcelRowSchema = z.array(ExcelCellSchema)
19+
const ExcelValuesSchema = z.union([
20+
z.string(),
21+
z.array(ExcelRowSchema),
22+
z.array(z.record(ExcelCellSchema)),
23+
])
24+
1625
const OneDriveUploadSchema = z.object({
1726
accessToken: z.string().min(1, 'Access token is required'),
1827
fileName: z.string().min(1, 'File name is required'),
1928
file: z.any().optional(), // UserFile object (optional for blank Excel creation)
2029
folderId: z.string().optional().nullable(),
2130
mimeType: z.string().optional(),
2231
// Optional Excel write-after-create inputs
23-
values: z.array(z.array(z.union([z.string(), z.number(), z.boolean(), z.null()]))).optional(),
32+
values: ExcelValuesSchema.optional(),
2433
})
2534

2635
export async function POST(request: NextRequest) {
@@ -46,6 +55,7 @@ export async function POST(request: NextRequest) {
4655

4756
const body = await request.json()
4857
const validatedData = OneDriveUploadSchema.parse(body)
58+
const excelValues = normalizeExcelValues(validatedData.values)
4959

5060
let fileBuffer: Buffer
5161
let mimeType: string
@@ -180,7 +190,7 @@ export async function POST(request: NextRequest) {
180190
// If this is an Excel creation and values were provided, write them using the Excel API
181191
let excelWriteResult: any | undefined
182192
const shouldWriteExcelContent =
183-
isExcelCreation && Array.isArray(validatedData.values) && validatedData.values.length > 0
193+
isExcelCreation && Array.isArray(excelValues) && excelValues.length > 0
184194

185195
if (shouldWriteExcelContent) {
186196
try {
@@ -232,7 +242,7 @@ export async function POST(request: NextRequest) {
232242
logger.warn(`[${requestId}] Error listing worksheets, using default Sheet1`, listError)
233243
}
234244

235-
let processedValues: any = validatedData.values || []
245+
let processedValues: any = excelValues || []
236246

237247
if (
238248
Array.isArray(processedValues) &&

apps/sim/blocks/blocks/onedrive.ts

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,11 @@ import { MicrosoftOneDriveIcon } from '@/components/icons'
22
import { createLogger } from '@/lib/logs/console/logger'
33
import type { BlockConfig } from '@/blocks/types'
44
import { AuthMode } from '@/blocks/types'
5-
import type { OneDriveResponse, OneDriveToolParams } from '@/tools/onedrive/types'
5+
import type { OneDriveResponse } from '@/tools/onedrive/types'
6+
import { normalizeExcelValuesForToolParams } from '@/tools/onedrive/utils'
67

78
const logger = createLogger('OneDriveBlock')
89

9-
type ExcelValues = OneDriveToolParams['values']
10-
11-
/**
12-
* Normalizes Excel values so that downstream tooling always receives an array.
13-
*/
14-
function normalizeExcelValues(values: unknown): ExcelValues | undefined {
15-
if (values === null || values === undefined || values === '') {
16-
return undefined
17-
}
18-
19-
if (Array.isArray(values)) {
20-
return values as ExcelValues
21-
}
22-
23-
if (typeof values === 'string') {
24-
const trimmed = values.trim()
25-
if (!trimmed) {
26-
return undefined
27-
}
28-
29-
try {
30-
const parsed = JSON.parse(trimmed)
31-
32-
if (!Array.isArray(parsed)) {
33-
throw new Error('Excel values must be an array of rows or array of objects')
34-
}
35-
36-
return parsed as ExcelValues
37-
} catch (_error) {
38-
throw new Error('Invalid JSON format for values')
39-
}
40-
}
41-
42-
throw new Error('Excel values must be an array of rows or array of objects')
43-
}
44-
4510
export const OneDriveBlock: BlockConfig<OneDriveResponse> = {
4611
type: 'onedrive',
4712
name: 'OneDrive',
@@ -387,9 +352,9 @@ export const OneDriveBlock: BlockConfig<OneDriveResponse> = {
387352
params: (params) => {
388353
const { credential, folderId, fileId, mimeType, values, downloadFileName, ...rest } = params
389354

390-
let normalizedValues: ExcelValues | undefined
355+
let normalizedValues: ReturnType<typeof normalizeExcelValuesForToolParams>
391356
if (values !== undefined) {
392-
normalizedValues = normalizeExcelValues(values)
357+
normalizedValues = normalizeExcelValuesForToolParams(values)
393358
}
394359

395360
return {

apps/sim/tools/onedrive/utils.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { OneDriveToolParams } from '@/tools/onedrive/types'
2+
3+
export type ExcelCell = string | number | boolean | null
4+
export type ExcelArrayValues = ExcelCell[][]
5+
export type ExcelObjectValues = Array<Record<string, ExcelCell>>
6+
export type NormalizedExcelValues = ExcelArrayValues | ExcelObjectValues
7+
8+
/**
9+
* Ensures Excel values are always represented as arrays before hitting downstream tooling.
10+
* Accepts JSON strings, array-of-arrays, or array-of-objects and normalizes them.
11+
*/
12+
export function normalizeExcelValues(values: unknown): NormalizedExcelValues | undefined {
13+
if (values === null || values === undefined) {
14+
return undefined
15+
}
16+
17+
if (typeof values === 'string') {
18+
const trimmed = values.trim()
19+
if (!trimmed) {
20+
return undefined
21+
}
22+
23+
try {
24+
const parsed = JSON.parse(trimmed)
25+
if (!Array.isArray(parsed)) {
26+
throw new Error('Excel values must be an array of rows or array of objects')
27+
}
28+
return parsed as NormalizedExcelValues
29+
} catch (_error) {
30+
throw new Error('Invalid JSON format for values')
31+
}
32+
}
33+
34+
if (Array.isArray(values)) {
35+
return values as NormalizedExcelValues
36+
}
37+
38+
throw new Error('Excel values must be an array of rows or array of objects')
39+
}
40+
41+
/**
42+
* Convenience helper for contexts that expect the narrower ToolParams typing.
43+
*/
44+
export function normalizeExcelValuesForToolParams(
45+
values: unknown
46+
): OneDriveToolParams['values'] | undefined {
47+
const normalized = normalizeExcelValues(values)
48+
return normalized as OneDriveToolParams['values'] | undefined
49+
}

0 commit comments

Comments
 (0)