Skip to content

Commit

Permalink
Merge branch 'tony/props-graph-syntax' of https://github.com/cosmicjs…
Browse files Browse the repository at this point in the history
…/cosmic-sdk-js into tony/canary
  • Loading branch information
tonyspiro committed Sep 11, 2024
2 parents f2cd49f + 404ea34 commit 79571fe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
node_modules
dist

test.*
test.*
46 changes: 39 additions & 7 deletions src/clients/bucket/lib/methodChaining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,50 @@ export default class MethodChaining {
}

props(props: string | Array<string>) {
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;
Expand Down
3 changes: 0 additions & 3 deletions src/clients/bucket/media/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
1 change: 0 additions & 1 deletion src/types/media.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { GenericObject } from './generic.types';
export type InsertMediaType = {
media: any;
folder?: string;
alt_text?: string;
metadata?: GenericObject;
trigger_webhook?: boolean;
};

0 comments on commit 79571fe

Please sign in to comment.