From 9d98cdfbc1996c1712c032ed1908e1372fc3fa14 Mon Sep 17 00:00:00 2001 From: Tony Spiro Date: Sun, 1 Sep 2024 20:30:42 -0700 Subject: [PATCH 1/2] add: props graph syntax --- .gitignore | 4 ++- src/clients/bucket/lib/methodChaining.ts | 46 ++++++++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index fa758be..2b86cec 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ .LSOverride node_modules -dist \ No newline at end of file +dist + +test.js \ No newline at end of file diff --git a/src/clients/bucket/lib/methodChaining.ts b/src/clients/bucket/lib/methodChaining.ts index 99d80ed..dee2439 100644 --- a/src/clients/bucket/lib/methodChaining.ts +++ b/src/clients/bucket/lib/methodChaining.ts @@ -6,18 +6,50 @@ export default class MethodChaining { } props(props: string | Array) { - let propStr = props; - if (Array.isArray(propStr)) { - propStr = propStr - .filter((prop) => typeof prop === 'string') + let propStr: string; + + if (typeof props === 'string') { + propStr = + props.startsWith('{') && props.endsWith('}') + ? this.parseGraphQLProps(props.slice(1, -1)) + : props; + } else if (Array.isArray(props)) { + propStr = props + .filter((prop): prop is string => typeof prop === 'string') .map((prop) => prop.trim()) - .filter((prop) => !!prop) - .toString(); + .filter(Boolean) + .join(','); + } else { + throw new Error('Invalid props type'); } - this.endpoint += `&props=${propStr}`; + this.endpoint += `&props=${encodeURIComponent(propStr)}`; return this; } + private parseGraphQLProps(propsString: string): string { + const lines = propsString + .split('\n') + .map((line) => line.trim()) + .filter(Boolean); + const result: string[] = []; + const currentPath: string[] = []; + + for (const line of lines) { + if (line.includes('{')) { + const [key] = line.split('{'); + if (key !== undefined) { + currentPath.push(key.trim()); + } + } else if (line === '}') { + currentPath.pop(); + } else { + result.push([...currentPath, line].join('.')); + } + } + + return result.join(','); + } + sort(sort: string) { this.endpoint += `&sort=${sort}`; return this; From 2918c43f677c6536945e12090e0bf7c182b73f21 Mon Sep 17 00:00:00 2001 From: Tony Spiro Date: Mon, 2 Sep 2024 10:08:10 -0700 Subject: [PATCH 2/2] edit: remove alt_text (another PR) --- src/clients/bucket/media/index.ts | 3 --- src/types/media.types.ts | 1 - 2 files changed, 4 deletions(-) diff --git a/src/clients/bucket/media/index.ts b/src/clients/bucket/media/index.ts index 5f58d19..0c3f721 100644 --- a/src/clients/bucket/media/index.ts +++ b/src/clients/bucket/media/index.ts @@ -48,9 +48,6 @@ export const mediaChainMethods = ( if (params.metadata) { data.append('metadata', JSON.stringify(params.metadata)); } - if (params.alt_text) { - data.append('alt_text', params.alt_text); - } if (params.trigger_webhook) { data.append('trigger_webhook', params.trigger_webhook.toString()); } diff --git a/src/types/media.types.ts b/src/types/media.types.ts index d1bf32e..c50f7fc 100644 --- a/src/types/media.types.ts +++ b/src/types/media.types.ts @@ -3,7 +3,6 @@ import { GenericObject } from './generic.types'; export type InsertMediaType = { media: any; folder?: string; - alt_text?: string; metadata?: GenericObject; trigger_webhook?: boolean; };