Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Props graph syntax #37

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -6,18 +6,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;
};
Loading