History

Add Google Photos preset with binary-safe uploads (#1137)

* feat(google): add Google Photos preset with binary uploads * test(e2e): cover Google Photos preset add flow * fix: address Google Photos upload review * fix: scope partial Google Photos bundles * fix: reject invalid nested binary uploads * fix: reject non-byte octet-stream bodies * fix: stop calling raw bytes an object * fix: base64 cosplay is not media bytes * Annotate bodyBase64 media type * test(google): update presets snapshot for Google Photos presets ---------

Zak committed 4 months ago

Commit 1a1f9aa1 parent ccfe20513 files · +899 −32Browse files

Unified · Split

13 files
  1. .changeset/google-photos-openapi-presets.md +6 −0
  2. e2e/scenarios/google-photos-preset-ui.test.ts +43 −0
  3. packages/plugins/google/src/react/AddGoogleSource.tsx +21 −7
  4. packages/plugins/google/src/react/source-plugin.ts +2 −2
  5. packages/plugins/google/src/sdk/__snapshots__/presets.test.ts.snap +8 −0
  6. packages/plugins/google/src/sdk/discovery.ts +120 −7
  7. packages/plugins/google/src/sdk/index.ts +5 −0
  8. packages/plugins/google/src/sdk/plugin.test.ts +213 −0
  9. packages/plugins/google/src/sdk/plugin.ts +34 −3
  10. packages/plugins/google/src/sdk/presets.ts +40 −0
  11. packages/plugins/openapi/src/sdk/extract.ts +38 −5
  12. packages/plugins/openapi/src/sdk/invoke.ts +120 −7
  13. packages/plugins/openapi/src/sdk/non-json-body.test.ts +249 −1
.changeset/google-photos-openapi-presets.md+6−0
@@ -0,0 +1,6 @@
1---
2"@executor-js/plugin-google": patch
3"@executor-js/plugin-openapi": patch
4---
5
6Add a Google Photos preset with raw upload support and binary-safe `bodyBase64` handling.
e2e/scenarios/google-photos-preset-ui.test.ts+43−0
@@ -0,0 +1,43 @@
1import { expect } from "@effect/vitest";
2import { Effect } from "effect";
3
4import { scenario } from "../src/scenario";
5import { Browser, Target } from "../src/services";
6
7scenario(
8 "Google Photos: the focused preset opens a Photos-scoped add flow",
9 {},
10 Effect.gen(function* () {
11 const target = yield* Target;
12 const browser = yield* Browser;
13 const identity = yield* target.newIdentity();
14
15 yield* browser.session(identity, async ({ page, step }) => {
16 await step("Find the Google Photos preset from the integrations picker", async () => {
17 await page.goto("/integrations", { waitUntil: "networkidle" });
18 await page.getByRole("button", { name: "Connect" }).click();
19 const dialog = page.getByRole("dialog", { name: "Connect an integration" });
20 await dialog.waitFor();
21 await dialog.getByPlaceholder(/Search or paste a URL/).fill("google photos");
22 await dialog.getByRole("link", { name: /^Google Photos\b/ }).waitFor();
23 });
24
25 await step("Open the Google Photos scoped add flow", async () => {
26 const dialog = page.getByRole("dialog", { name: "Connect an integration" });
27 await dialog.getByRole("link", { name: /^Google Photos\b/ }).click();
28 await page.waitForURL(/\/integrations\/add\/google/);
29 await page.getByRole("heading", { name: "Add Google" }).waitFor();
30 });
31
32 await step("The Photos preset defaults to the focused namespace and products", async () => {
33 await page.locator('input[value="Google Photos"]').waitFor();
34 await page.locator('input[value="google_photos"]').waitFor();
35 await page.getByText("Google Photos Library").first().waitFor();
36 await page.getByText("Google Photos Picker").first().waitFor();
37 await page.getByText("2 Google APIs").waitFor();
38 expect(await page.locator('input[value="Google"]').count()).toBe(0);
39 expect(await page.locator('input[value="google"]').count()).toBe(0);
40 });
41 });
42 }),
43);
packages/plugins/google/src/react/AddGoogleSource.tsx+21−7
@@ -20,7 +20,12 @@ import { OpenApiSourceDetailsFields } from "@executor-js/plugin-openapi/react"; · 19 unmodified lines
20 20
21import { addGoogleBundle } from "./atoms";21import { addGoogleBundle } from "./atoms";
22import { GoogleProductPicker } from "./GoogleProductPicker";22import { GoogleProductPicker } from "./GoogleProductPicker";
23import { googleOpenApiPresets, type GoogleOpenApiPreset } from "../sdk/presets";23import {
24 GOOGLE_PHOTOS_PRESET_ID,
25 googleOpenApiPresets,
26 googlePhotosPresetIds,
27 type GoogleOpenApiPreset,
28} from "../sdk/presets";
24 29
25const GOOGLE_BUNDLE_FAVICON = "https://fonts.gstatic.com/s/i/productlogos/googleg/v6/192px.svg";30const GOOGLE_BUNDLE_FAVICON = "https://fonts.gstatic.com/s/i/productlogos/googleg/v6/192px.svg";
26 31
@@ -43,10 +48,12 @@ const googleBundleUrls = ( · 16 unmodified lines
43export default function AddGoogleSource(props: {48export default function AddGoogleSource(props: {
44 onComplete: (slug?: string) => void;49 onComplete: (slug?: string) => void;
45 onCancel: () => void;50 onCancel: () => void;
51 initialPreset?: string;
46 initialNamespace?: string;52 initialNamespace?: string;
47}) {53}) {
54 const isGooglePhotosPreset = props.initialPreset === GOOGLE_PHOTOS_PRESET_ID;
48 const [selectedPresetIds, setSelectedPresetIds] = useState<ReadonlySet<string>>(55 const [selectedPresetIds, setSelectedPresetIds] = useState<ReadonlySet<string>>(
49 googleBundleDefaultPresetIds,56 isGooglePhotosPreset ? new Set(googlePhotosPresetIds) : googleBundleDefaultPresetIds,
50 );57 );
51 const [customDiscoveryUrls, setCustomDiscoveryUrls] = useState<readonly string[]>([]);58 const [customDiscoveryUrls, setCustomDiscoveryUrls] = useState<readonly string[]>([]);
52 const [baseUrl, setBaseUrl] = useState("");59 const [baseUrl, setBaseUrl] = useState("");
@@ -55,8 +62,9 @@ export default function AddGoogleSource(props: { · 2 unmodified lines
55 const [addError, setAddError] = useState<string | null>(null);62 const [addError, setAddError] = useState<string | null>(null);
56 63
57 const identity = useIntegrationIdentity({64 const identity = useIntegrationIdentity({
58 fallbackName: "Google",65 fallbackName: isGooglePhotosPreset ? "Google Photos" : "Google",
59 fallbackNamespace: props.initialNamespace ?? "google",66 fallbackNamespace:
67 props.initialNamespace ?? (isGooglePhotosPreset ? "google_photos" : "google"),
60 });68 });
61 69
62 const bundleDiscoveryUrls = useMemo(70 const bundleDiscoveryUrls = useMemo(
@@ -87,9 +95,15 @@ export default function AddGoogleSource(props: { · 24 unmodified lines
87 95
88 const doAdd = useAtomSet(addGoogleBundle, { mode: "promiseExit" });96 const doAdd = useAtomSet(addGoogleBundle, { mode: "promiseExit" });
89 97
90 const resolvedSourceId = slugifyNamespace(identity.namespace) || "google";98 const resolvedSourceId =
91 const resolvedDisplayName = identity.name.trim() || "Google";99 slugifyNamespace(identity.namespace) || (isGooglePhotosPreset ? "google_photos" : "google");
92 const resolvedDescription = descriptionDraft ?? "Google APIs";100 const resolvedDisplayName =
101 identity.name.trim() || (isGooglePhotosPreset ? "Google Photos" : "Google");
102 const resolvedDescription =
103 descriptionDraft ??
104 (isGooglePhotosPreset
105 ? "Google Photos albums, uploads, app-created media, and selected picker media."
106 : "Google APIs");
93 const slugAlreadyExists = useSlugAlreadyExists(resolvedSourceId);107 const slugAlreadyExists = useSlugAlreadyExists(resolvedSourceId);
94 const canAdd = bundleDiscoveryUrls.length > 0 && !slugAlreadyExists;108 const canAdd = bundleDiscoveryUrls.length > 0 && !slugAlreadyExists;
95 109
packages/plugins/google/src/react/source-plugin.ts+2−2
@@ -1,6 +1,6 @@
1import { lazy } from "react";1import { lazy } from "react";
2import type { IntegrationPlugin } from "@executor-js/sdk/client";2import type { IntegrationPlugin } from "@executor-js/sdk/client";
3import { googleOpenApiBundlePreset } from "../sdk/presets";3import { googleOpenApiBundlePreset, googlePhotosOpenApiBundlePreset } from "../sdk/presets";
4 4
5const importAdd = () => import("./AddGoogleSource");5const importAdd = () => import("./AddGoogleSource");
6const importAccounts = () => import("./GoogleAccountsPanel");6const importAccounts = () => import("./GoogleAccountsPanel");
@@ -10,7 +10,7 @@ export const googleIntegrationPlugin: IntegrationPlugin = { · 3 unmodified lines
10 label: "Google",10 label: "Google",
11 add: lazy(importAdd),11 add: lazy(importAdd),
12 accounts: lazy(importAccounts),12 accounts: lazy(importAccounts),
13 presets: [googleOpenApiBundlePreset],13 presets: [googleOpenApiBundlePreset, googlePhotosOpenApiBundlePreset],
14 preload: () => {14 preload: () => {
15 void importAdd();15 void importAdd();
16 void importAccounts();16 void importAccounts();
packages/plugins/google/src/sdk/__snapshots__/presets.test.ts.snap+8−0
@@ -38,6 +38,14 @@ exports[`classifies every Google service for bundle OAuth UX 1`] = ` · 37 unmodified lines
38 "id": "google-people",38 "id": "google-people",
39 "oauthAudience": "standard-user",39 "oauthAudience": "standard-user",
40 },40 },
41 {
42 "id": "google-photos-library",
43 "oauthAudience": "advanced-user",
44 },
45 {
46 "id": "google-photos-picker",
47 "oauthAudience": "advanced-user",
48 },
41 {49 {
42 "id": "google-chat",50 "id": "google-chat",
43 "oauthAudience": "workspace-admin",51 "oauthAudience": "workspace-admin",
packages/plugins/google/src/sdk/discovery.ts+120−7
@@ -66,12 +66,13 @@ type OpenApiOperationObject = { · 65 unmodified lines
66 readonly servers?: readonly { readonly url: string }[];66 readonly servers?: readonly { readonly url: string }[];
67 readonly parameters: readonly OpenApiParameterObject[];67 readonly parameters: readonly OpenApiParameterObject[];
68 readonly requestBody?: {68 readonly requestBody?: {
69 readonly required: false;69 readonly required: boolean;
70 readonly content: {70 readonly content: Record<
71 readonly "application/json": {71 string,
72 {
72 readonly schema: OpenApiSchemaObject;73 readonly schema: OpenApiSchemaObject;
73 };74 }
74 };75 >;
75 };76 };
76 readonly responses: {77 readonly responses: {
77 readonly "200": {78 readonly "200": {
@@ -605,6 +606,7 @@ const buildDiscoveryOperation = (input: { · 527 unmodified lines
605 readonly method: DiscoveryMethod;606 readonly method: DiscoveryMethod;
606 readonly toolPath: string;607 readonly toolPath: string;
607 readonly pathTemplate: string;608 readonly pathTemplate: string;
609 readonly oauthScopes?: readonly string[];
608 readonly schemaNameForRef?: (name: string) => string;610 readonly schemaNameForRef?: (name: string) => string;
609 readonly serverUrl?: string;611 readonly serverUrl?: string;
610 readonly tags?: readonly string[];612 readonly tags?: readonly string[];
@@ -619,7 +621,7 @@ const buildDiscoveryOperation = (input: { · 8 unmodified lines
619 if (parameter.location) mergedParameters.set(name, parameter);621 if (parameter.location) mergedParameters.set(name, parameter);
620 }622 }
621 623
622 const methodScopes = input.method.scopes ?? [];624 const methodScopes = input.oauthScopes ?? input.method.scopes ?? [];
623 const methodDescription = Option.getOrUndefined(input.method.description);625 const methodDescription = Option.getOrUndefined(input.method.description);
624 const schemaNameForRef = input.schemaNameForRef ?? identitySchemaName;626 const schemaNameForRef = input.schemaNameForRef ?? identitySchemaName;
625 627
@@ -674,6 +676,14 @@ const buildDiscoveryOperation = (input: { · 48 unmodified lines
674};676};
675 677
676const GOOGLE_OAUTH_SECURITY_SCHEME = "googleOAuth2";678const GOOGLE_OAUTH_SECURITY_SCHEME = "googleOAuth2";
679const GOOGLE_PHOTOS_LIBRARY_SERVICE = "photoslibrary";
680const GOOGLE_PHOTOS_PICKER_SERVICE = "photospicker";
681const GOOGLE_PHOTOS_APPENDONLY_SCOPE = "https://www.googleapis.com/auth/photoslibrary.appendonly";
682const GOOGLE_PHOTOS_UPLOAD_TOOL_PATH = "photoslibrary.mediaItems.upload";
683const GOOGLE_PHOTOS_UPLOAD_PATH = "/uploads";
684
685const isGooglePhotosService = (service: string): boolean =>
686 service === GOOGLE_PHOTOS_LIBRARY_SERVICE || service === GOOGLE_PHOTOS_PICKER_SERVICE;
677 687
678/** The v2 oauth auth template for a Google-discovery integration. The spec688/** The v2 oauth auth template for a Google-discovery integration. The spec
679 * itself carries the matching `securitySchemes.googleOAuth2` entry; this is the689 * itself carries the matching `securitySchemes.googleOAuth2` entry; this is the
@@ -695,6 +705,72 @@ const googleOauthTemplate = (scopes: Record<string, string>): readonly Authentic · 15 unmodified lines
695 },705 },
696 ];706 ];
697 707
708const googlePhotosUploadOperation = (input: {
709 readonly toolPath: string;
710 readonly oauthScopes: readonly string[];
711 readonly serverUrl: string;
712 readonly tags?: readonly string[];
713}): OpenApiOperationObject => ({
714 operationId: input.toolPath,
715 "x-executor-toolPath": input.toolPath,
716 "x-executor-pathTemplate": GOOGLE_PHOTOS_UPLOAD_PATH,
717 ...(input.tags && input.tags.length > 0 ? { tags: input.tags } : {}),
718 description:
719 "Uploads raw photo or video bytes to Google Photos and returns a plain-text upload token. Call mediaItems.batchCreate with the returned token to create the media item.",
720 servers: [{ url: input.serverUrl }],
721 parameters: [
722 {
723 name: "X-Goog-Upload-File-Name",
724 in: "header",
725 required: true,
726 description: "File name Google Photos should associate with the uploaded bytes.",
727 schema: { type: "string" },
728 },
729 {
730 name: "X-Goog-Upload-Protocol",
731 in: "header",
732 required: true,
733 description: "Google Photos raw upload protocol. Set to raw.",
734 schema: { type: "string", enum: ["raw"], default: "raw" },
735 },
736 {
737 name: "X-Goog-Upload-Content-Type",
738 in: "header",
739 required: false,
740 description: "MIME type of the uploaded media, for example image/jpeg or video/mp4.",
741 schema: { type: "string" },
742 },
743 ],
744 requestBody: {
745 required: true,
746 content: {
747 "application/octet-stream": {
748 schema: { type: "string", format: "binary" },
749 },
750 },
751 },
752 responses: {
753 "200": {
754 description: "Successful response",
755 content: {
756 "text/plain": {
757 schema: { type: "string" },
758 },
759 },
760 },
761 },
762 ...(input.oauthScopes.length > 0 ? { security: [{ googleOAuth2: input.oauthScopes }] } : {}),
763 "x-google-scopes": input.oauthScopes,
764});
765
766const hasOperation = (
767 paths: Record<string, Record<string, OpenApiOperationObject>>,
768 operationId: string,
769): boolean =>
770 Object.values(paths).some((pathItem) =>
771 Object.values(pathItem).some((operation) => operation.operationId === operationId),
772 );
773
698export const convertGoogleDiscoveryToOpenApi = Effect.fn("OpenApi.convertGoogleDiscovery")(774export const convertGoogleDiscoveryToOpenApi = Effect.fn("OpenApi.convertGoogleDiscovery")(
699 function* (input: { readonly discoveryUrl: string; readonly documentText: string }) {775 function* (input: { readonly discoveryUrl: string; readonly documentText: string }) {
700 const parsed = yield* parseJson(input.documentText).pipe(776 const parsed = yield* parseJson(input.documentText).pipe(
@@ -738,6 +814,18 @@ export const convertGoogleDiscoveryToOpenApi = Effect.fn("OpenApi.convertGoogleD · 37 unmodified lines
738 });814 });
739 }815 }
740 816
817 if (
818 service === GOOGLE_PHOTOS_LIBRARY_SERVICE &&
819 !hasOperation(paths, GOOGLE_PHOTOS_UPLOAD_TOOL_PATH)
820 ) {
821 paths[GOOGLE_PHOTOS_UPLOAD_PATH] ??= {};
822 paths[GOOGLE_PHOTOS_UPLOAD_PATH]!.post = googlePhotosUploadOperation({
823 toolPath: GOOGLE_PHOTOS_UPLOAD_TOOL_PATH,
824 oauthScopes: [GOOGLE_PHOTOS_APPENDONLY_SCOPE],
825 serverUrl: baseUrl,
826 });
827 }
828
741 const scopes = compactDiscoveryScopeMap(discoveryScopes(document));829 const scopes = compactDiscoveryScopeMap(discoveryScopes(document));
742 const authenticationTemplate = googleOauthTemplate(scopes);830 const authenticationTemplate = googleOauthTemplate(scopes);
743 831
@@ -798,6 +886,7 @@ export const convertGoogleDiscoveryBundleToOpenApi = Effect.fn( · 54 unmodified lines
798 "OpenApi.convertGoogleDiscoveryBundle",886 "OpenApi.convertGoogleDiscoveryBundle",
799)(function* (input: {887)(function* (input: {
800 readonly documents: readonly { readonly discoveryUrl: string; readonly documentText: string }[];888 readonly documents: readonly { readonly discoveryUrl: string; readonly documentText: string }[];
889 readonly consentScopes?: readonly string[];
801}) {890}) {
802 if (input.documents.length === 0) {891 if (input.documents.length === 0) {
803 return yield* new OpenApiParseError({892 return yield* new OpenApiParseError({
@@ -829,12 +918,16 @@ export const convertGoogleDiscoveryBundleToOpenApi = Effect.fn( · 25 unmodified lines
829 const paths: Record<string, Record<string, OpenApiOperationObject>> = {};918 const paths: Record<string, Record<string, OpenApiOperationObject>> = {};
830 const schemas: Record<string, OpenApiSchemaObject> = {};919 const schemas: Record<string, OpenApiSchemaObject> = {};
831 const rawScopes: Record<string, string> = {};920 const rawScopes: Record<string, string> = {};
921 const consentScopeSet = input.consentScopes ? new Set(input.consentScopes) : null;
832 922
833 for (const info of infos) {923 for (const info of infos) {
834 const schemaPrefix = schemaComponentPart(`${info.service}_${info.version}`);924 const schemaPrefix = schemaComponentPart(`${info.service}_${info.version}`);
835 const schemaNameForRef = (name: string) => `${schemaPrefix}_${schemaComponentPart(name)}`;925 const schemaNameForRef = (name: string) => `${schemaPrefix}_${schemaComponentPart(name)}`;
926 const scopeDescriptions = discoveryScopes(info.document);
927 const filterPhotosScopes = consentScopeSet !== null && isGooglePhotosService(info.service);
836 928
837 for (const [scope, description] of Object.entries(discoveryScopes(info.document))) {929 for (const [scope, description] of Object.entries(scopeDescriptions)) {
930 if (filterPhotosScopes && !consentScopeSet.has(scope)) continue;
838 rawScopes[scope] ??= description;931 rawScopes[scope] ??= description;
839 }932 }
840 933
@@ -846,6 +939,11 @@ export const convertGoogleDiscoveryBundleToOpenApi = Effect.fn( · 5 unmodified lines
846 const methodId = Option.getOrUndefined(method.id);939 const methodId = Option.getOrUndefined(method.id);
847 const rawPathTemplate = Option.getOrUndefined(method.path);940 const rawPathTemplate = Option.getOrUndefined(method.path);
848 if (!methodId || !rawPathTemplate || !method.httpMethod) continue;941 if (!methodId || !rawPathTemplate || !method.httpMethod) continue;
942 const methodScopes = method.scopes ?? [];
943 const oauthScopes = filterPhotosScopes
944 ? methodScopes.filter((scope) => consentScopeSet.has(scope))
945 : methodScopes;
946 if (filterPhotosScopes && methodScopes.length > 0 && oauthScopes.length === 0) continue;
849 947
850 const toolPath = methodId;948 const toolPath = methodId;
851 const wirePath = rawPathTemplate.startsWith("/") ? rawPathTemplate : `/${rawPathTemplate}`;949 const wirePath = rawPathTemplate.startsWith("/") ? rawPathTemplate : `/${rawPathTemplate}`;
@@ -859,11 +957,26 @@ export const convertGoogleDiscoveryBundleToOpenApi = Effect.fn( · 7 unmodified lines
859 method,957 method,
860 toolPath,958 toolPath,
861 pathTemplate: wirePath,959 pathTemplate: wirePath,
960 oauthScopes,
862 schemaNameForRef,961 schemaNameForRef,
863 serverUrl: info.baseUrl,962 serverUrl: info.baseUrl,
864 tags: [info.title],963 tags: [info.title],
865 });964 });
866 }965 }
966
967 if (
968 info.service === GOOGLE_PHOTOS_LIBRARY_SERVICE &&
969 (!consentScopeSet || consentScopeSet.has(GOOGLE_PHOTOS_APPENDONLY_SCOPE)) &&
970 !hasOperation(paths, GOOGLE_PHOTOS_UPLOAD_TOOL_PATH)
971 ) {
972 paths[GOOGLE_PHOTOS_UPLOAD_PATH] ??= {};
973 paths[GOOGLE_PHOTOS_UPLOAD_PATH]!.post = googlePhotosUploadOperation({
974 toolPath: GOOGLE_PHOTOS_UPLOAD_TOOL_PATH,
975 oauthScopes: [GOOGLE_PHOTOS_APPENDONLY_SCOPE],
976 serverUrl: info.baseUrl,
977 tags: [info.title],
978 });
979 }
867 }980 }
868 981
869 const scopes = compactDiscoveryScopeMap(rawScopes);982 const scopes = compactDiscoveryScopeMap(rawScopes);
packages/plugins/google/src/sdk/index.ts+5−0
@@ -8,6 +8,11 @@ export { · 7 unmodified lines
8export {8export {
9 googleOpenApiBundlePreset,9 googleOpenApiBundlePreset,
10 googleOpenApiPresets,10 googleOpenApiPresets,
11 googlePhotosOpenApiBundlePreset,
12 googlePhotosOpenApiPresets,
13 googlePhotosPresetIds,
14 GOOGLE_PHOTOS_ICON,
15 GOOGLE_PHOTOS_PRESET_ID,
11 googleStandardUserOAuthPresets,16 googleStandardUserOAuthPresets,
12 googleOAuthConsentScopes,17 googleOAuthConsentScopes,
13 googleOAuthConsentScopesForPreset,18 googleOAuthConsentScopesForPreset,
packages/plugins/google/src/sdk/plugin.test.ts+213−0
@@ -34,6 +34,8 @@ import { googlePlugin } from "./plugin"; · 33 unmodified lines
34const CALENDAR_URL = "https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest";34const CALENDAR_URL = "https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest";
35const GMAIL_URL = "https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest";35const GMAIL_URL = "https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest";
36const DRIVE_URL = "https://www.googleapis.com/discovery/v1/apis/drive/v3/rest";36const DRIVE_URL = "https://www.googleapis.com/discovery/v1/apis/drive/v3/rest";
37const PHOTOS_LIBRARY_URL = "https://www.googleapis.com/discovery/v1/apis/photoslibrary/v1/rest";
38const PHOTOS_PICKER_URL = "https://www.googleapis.com/discovery/v1/apis/photospicker/v1/rest";
37 39
38const calendarDoc = {40const calendarDoc = {
39 name: "calendar",41 name: "calendar",
@@ -141,12 +143,93 @@ const driveDoc = { · 101 unmodified lines
141 },143 },
142};144};
143 145
146const photosLibraryDoc = {
147 name: "photoslibrary",
148 version: "v1",
149 title: "Google Photos Library API",
150 rootUrl: "https://photoslibrary.googleapis.com/",
151 servicePath: "v1/",
152 auth: {
153 oauth2: {
154 scopes: {
155 "https://www.googleapis.com/auth/photoslibrary": {
156 description: "Manage the full Google Photos library",
157 },
158 "https://www.googleapis.com/auth/photoslibrary.appendonly": {
159 description: "Upload to Google Photos",
160 },
161 "https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata": {
162 description: "Read app-created Google Photos media",
163 },
164 },
165 },
166 },
167 resources: {
168 albums: {
169 methods: {
170 list: {
171 id: "photoslibrary.albums.list",
172 httpMethod: "GET",
173 path: "albums",
174 scopes: ["https://www.googleapis.com/auth/photoslibrary"],
175 parameters: {},
176 },
177 },
178 },
179 mediaItems: {
180 methods: {
181 search: {
182 id: "photoslibrary.mediaItems.search",
183 httpMethod: "POST",
184 path: "mediaItems:search",
185 scopes: ["https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata"],
186 parameters: {},
187 },
188 },
189 },
190 },
191 schemas: {},
192};
193
194const photosPickerDoc = {
195 name: "photospicker",
196 version: "v1",
197 title: "Google Photos Picker API",
198 rootUrl: "https://photospicker.googleapis.com/",
199 servicePath: "v1/",
200 auth: {
201 oauth2: {
202 scopes: {
203 "https://www.googleapis.com/auth/photospicker.mediaitems.readonly": {
204 description: "Read selected Google Photos media",
205 },
206 },
207 },
208 },
209 resources: {
210 mediaItems: {
211 methods: {
212 list: {
213 id: "photospicker.mediaItems.list",
214 httpMethod: "GET",
215 path: "mediaItems",
216 scopes: ["https://www.googleapis.com/auth/photospicker.mediaitems.readonly"],
217 parameters: {},
218 },
219 },
220 },
221 },
222 schemas: {},
223};
224
144const toJson = (value: unknown): string => JSON.stringify(value);225const toJson = (value: unknown): string => JSON.stringify(value);
145 226
146const DISCOVERY_BODIES: Readonly<Record<string, string>> = {227const DISCOVERY_BODIES: Readonly<Record<string, string>> = {
147 [CALENDAR_URL]: toJson(calendarDoc),228 [CALENDAR_URL]: toJson(calendarDoc),
148 [GMAIL_URL]: toJson(gmailDoc),229 [GMAIL_URL]: toJson(gmailDoc),
149 [DRIVE_URL]: toJson(driveDoc),230 [DRIVE_URL]: toJson(driveDoc),
231 [PHOTOS_LIBRARY_URL]: toJson(photosLibraryDoc),
232 [PHOTOS_PICKER_URL]: toJson(photosPickerDoc),
150};233};
151 234
152// A stub HTTP client that serves the canned Discovery document for whichever235// A stub HTTP client that serves the canned Discovery document for whichever
@@ -174,6 +257,16 @@ const bundlePlugins = () => · 21 unmodified lines
174 [googlePlugin({ httpClientLayer: discoveryHttpClientLayer }), memoryCredentialsPlugin()] as const;257 [googlePlugin({ httpClientLayer: discoveryHttpClientLayer }), memoryCredentialsPlugin()] as const;
175 258
176describe("Google bundle add flow", () => {259describe("Google bundle add flow", () => {
260 it("SDK catalog includes the Google Photos focused preset", () => {
261 const presetIds =
262 googlePlugin({ httpClientLayer: discoveryHttpClientLayer }).integrationPresets?.map(
263 (preset) => preset.id,
264 ) ?? [];
265
266 expect(presetIds).toContain("google");
267 expect(presetIds).toContain("google-photos");
268 });
269
177 it.effect("rejects lookalike Discovery hosts before fetching bundle documents", () =>270 it.effect("rejects lookalike Discovery hosts before fetching bundle documents", () =>
178 Effect.scoped(271 Effect.scoped(
179 Effect.gen(function* () {272 Effect.gen(function* () {
@@ -266,4 +359,124 @@ describe("Google bundle add flow", () => { · 86 unmodified lines
266 }),359 }),
267 ),360 ),
268 );361 );
362
363 it.effect("addBundle constrains Google Photos to the preset scopes and upload tool", () =>
364 Effect.scoped(
365 Effect.gen(function* () {
366 const executor = yield* createExecutor(makeTestConfig({ plugins: bundlePlugins() }));
367
368 yield* executor.google.addBundle({
369 urls: [
370 PHOTOS_LIBRARY_URL,
371 "https://photospicker.googleapis.com/$discovery/rest?version=v1",
372 ],
373 slug: "google_photos",
374 name: "Google Photos",
375 });
376
377 const config = yield* executor.google.getConfig("google_photos");
378 const oauth = config?.authenticationTemplate?.find((entry) => entry.kind === "oauth2");
379 expect(oauth?.kind === "oauth2" ? [...oauth.scopes].sort() : undefined).toEqual(
380 [
381 "https://www.googleapis.com/auth/photoslibrary.appendonly",
382 "https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata",
383 "https://www.googleapis.com/auth/photospicker.mediaitems.readonly",
384 ].sort(),
385 );
386
387 yield* executor.connections.create({
388 owner: "org",
389 name: ConnectionName.make("main"),
390 integration: IntegrationSlug.make("google_photos"),
391 template: AuthTemplateSlug.make("googleOAuth2"),
392 value: "token-xyz",
393 });
394
395 const toolNames = (yield* executor.tools.list()).map((tool) => String(tool.name));
396 expect(toolNames).toContain("photoslibrary.mediaItems.upload");
397 expect(toolNames).toContain("photoslibrary.mediaItems.search");
398 expect(toolNames).toContain("photospicker.mediaItems.list");
399 expect(toolNames).not.toContain("photoslibrary.albums.list");
400 }),
401 ),
402 );
403
404 it.effect("addBundle keeps Google Photos scoped when combined with another API", () =>
405 Effect.scoped(
406 Effect.gen(function* () {
407 const executor = yield* createExecutor(makeTestConfig({ plugins: bundlePlugins() }));
408
409 yield* executor.google.addBundle({
410 urls: [CALENDAR_URL, PHOTOS_LIBRARY_URL, PHOTOS_PICKER_URL],
411 slug: "google_photos_calendar",
412 name: "Google Photos and Calendar",
413 });
414
415 const config = yield* executor.google.getConfig("google_photos_calendar");
416 const oauth = config?.authenticationTemplate?.find((entry) => entry.kind === "oauth2");
417 expect(oauth?.kind === "oauth2" ? [...oauth.scopes].sort() : undefined).toEqual(
418 [
419 "https://www.googleapis.com/auth/calendar",
420 "https://www.googleapis.com/auth/photoslibrary.appendonly",
421 "https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata",
422 "https://www.googleapis.com/auth/photospicker.mediaitems.readonly",
423 ].sort(),
424 );
425
426 yield* executor.connections.create({
427 owner: "org",
428 name: ConnectionName.make("main"),
429 integration: IntegrationSlug.make("google_photos_calendar"),
430 template: AuthTemplateSlug.make("googleOAuth2"),
431 value: "token-xyz",
432 });
433
434 const toolNames = (yield* executor.tools.list()).map((tool) => String(tool.name));
435 expect(toolNames).toContain("calendar.events.list");
436 expect(toolNames).toContain("photoslibrary.mediaItems.upload");
437 expect(toolNames).toContain("photoslibrary.mediaItems.search");
438 expect(toolNames).toContain("photospicker.mediaItems.list");
439 expect(toolNames).not.toContain("photoslibrary.albums.list");
440 }),
441 ),
442 );
443
444 it.effect("addBundle scopes a partial Google Photos bundle when mixed with another API", () =>
445 Effect.scoped(
446 Effect.gen(function* () {
447 const executor = yield* createExecutor(makeTestConfig({ plugins: bundlePlugins() }));
448
449 yield* executor.google.addBundle({
450 urls: [CALENDAR_URL, PHOTOS_LIBRARY_URL],
451 slug: "google_photos_library_calendar",
452 name: "Google Photos Library and Calendar",
453 });
454
455 const config = yield* executor.google.getConfig("google_photos_library_calendar");
456 const oauth = config?.authenticationTemplate?.find((entry) => entry.kind === "oauth2");
457 expect(oauth?.kind === "oauth2" ? [...oauth.scopes].sort() : undefined).toEqual(
458 [
459 "https://www.googleapis.com/auth/calendar",
460 "https://www.googleapis.com/auth/photoslibrary.appendonly",
461 "https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata",
462 ].sort(),
463 );
464
465 yield* executor.connections.create({
466 owner: "org",
467 name: ConnectionName.make("main"),
468 integration: IntegrationSlug.make("google_photos_library_calendar"),
469 template: AuthTemplateSlug.make("googleOAuth2"),
470 value: "token-xyz",
471 });
472
473 const toolNames = (yield* executor.tools.list()).map((tool) => String(tool.name));
474 expect(toolNames).toContain("calendar.events.list");
475 expect(toolNames).toContain("photoslibrary.mediaItems.upload");
476 expect(toolNames).toContain("photoslibrary.mediaItems.search");
477 expect(toolNames).not.toContain("photospicker.mediaItems.list");
478 expect(toolNames).not.toContain("photoslibrary.albums.list");
479 }),
480 ),
481 );
269});482});
packages/plugins/google/src/sdk/plugin.ts+34−3
@@ -36,7 +36,12 @@ import { · 35 unmodified lines
36 normalizeGoogleDiscoveryUrl,36 normalizeGoogleDiscoveryUrl,
37} from "./discovery";37} from "./discovery";
38import { decodeGoogleIntegrationConfig, type GoogleIntegrationConfig } from "./config";38import { decodeGoogleIntegrationConfig, type GoogleIntegrationConfig } from "./config";
39import { googleOpenApiBundlePreset } from "./presets";39import {
40 googleOAuthConsentScopesForPreset,
41 googleOpenApiBundlePreset,
42 googlePhotosOpenApiBundlePreset,
43 googlePhotosOpenApiPresets,
44} from "./presets";
40 45
41export interface GoogleBundleConfig {46export interface GoogleBundleConfig {
42 readonly urls: readonly string[];47 readonly urls: readonly string[];
@@ -68,6 +73,24 @@ export interface GooglePluginOptions { · 25 unmodified lines
68 73
69const DEFAULT_GOOGLE_SLUG = "google";74const DEFAULT_GOOGLE_SLUG = "google";
70 75
76const googlePhotosBundlePresetIdByUrl = new Map(
77 googlePhotosOpenApiPresets.flatMap((preset) =>
78 preset.url ? [[normalizeGoogleDiscoveryUrl(preset.url) ?? preset.url, preset.id] as const] : [],
79 ),
80);
81
82const googlePhotosBundleConsentScopes = (
83 urls: readonly string[],
84): readonly string[] | undefined => {
85 const normalized = new Set(urls);
86 const presetIds = [...googlePhotosBundlePresetIdByUrl.entries()].flatMap(([url, presetId]) =>
87 normalized.has(url) ? [presetId] : [],
88 );
89 return presetIds.length > 0
90 ? presetIds.flatMap((presetId) => googleOAuthConsentScopesForPreset(presetId))
91 : undefined;
92};
93
71const fetchGoogleBundleConversion = (94const fetchGoogleBundleConversion = (
72 urls: readonly string[],95 urls: readonly string[],
73 httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>,96 httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>,
@@ -80,7 +103,15 @@ const fetchGoogleBundleConversion = ( · 6 unmodified lines
80 Effect.map((documentText) => ({ discoveryUrl: url, documentText })),103 Effect.map((documentText) => ({ discoveryUrl: url, documentText })),
81 ),104 ),
82 { concurrency: 4 },105 { concurrency: 4 },
83 ).pipe(Effect.flatMap((documents) => convertGoogleDiscoveryBundleToOpenApi({ documents })));106 ).pipe(
107 Effect.flatMap((documents) => {
108 const consentScopes = googlePhotosBundleConsentScopes(urls);
109 return convertGoogleDiscoveryBundleToOpenApi({
110 documents,
111 ...(consentScopes ? { consentScopes } : {}),
112 });
113 }),
114 );
84 115
85const uniqueUrls = (urls: readonly string[]): readonly string[] => [116const uniqueUrls = (urls: readonly string[]): readonly string[] => [
86 ...new Set(urls.flatMap((url) => normalizeGoogleDiscoveryUrl(url) ?? [])),117 ...new Set(urls.flatMap((url) => normalizeGoogleDiscoveryUrl(url) ?? [])),
@@ -292,7 +323,7 @@ export type GooglePluginExtension = ReturnType<typeof makeGooglePluginExtension> · 205 unmodified lines
292export const googlePlugin = definePlugin((options?: GooglePluginOptions) => ({323export const googlePlugin = definePlugin((options?: GooglePluginOptions) => ({
293 id: "google" as const,324 id: "google" as const,
294 packageName: "@executor-js/plugin-google",325 packageName: "@executor-js/plugin-google",
295 integrationPresets: [googleOpenApiBundlePreset],326 integrationPresets: [googleOpenApiBundlePreset, googlePhotosOpenApiBundlePreset],
296 storage: (deps): OpenapiStore => makeDefaultOpenapiStore(deps),327 storage: (deps): OpenapiStore => makeDefaultOpenapiStore(deps),
297 328
298 extension: (ctx: PluginCtx<OpenapiStore>) => makeGooglePluginExtension(options, ctx),329 extension: (ctx: PluginCtx<OpenapiStore>) => makeGooglePluginExtension(options, ctx),
packages/plugins/google/src/sdk/presets.ts+40−0
@@ -24,6 +24,9 @@ const gd = (service: string, version: string) => · 23 unmodified lines
24 24
25const GOOGLE_G = "https://fonts.gstatic.com/s/i/productlogos/googleg/v6/192px.svg";25const GOOGLE_G = "https://fonts.gstatic.com/s/i/productlogos/googleg/v6/192px.svg";
26export const GOOGLE_BUNDLE_PRESET_ID = "google";26export const GOOGLE_BUNDLE_PRESET_ID = "google";
27export const GOOGLE_PHOTOS_PRESET_ID = "google-photos";
28export const GOOGLE_PHOTOS_ICON =
29 "https://www.gstatic.com/images/branding/product/2x/photos_96dp.png";
27 30
28export const googleOpenApiBundlePreset: GooglePreset = {31export const googleOpenApiBundlePreset: GooglePreset = {
29 id: GOOGLE_BUNDLE_PRESET_ID,32 id: GOOGLE_BUNDLE_PRESET_ID,
@@ -33,6 +36,14 @@ export const googleOpenApiBundlePreset: GooglePreset = { · 3 unmodified lines
33 featured: true,36 featured: true,
34};37};
35 38
39export const googlePhotosOpenApiBundlePreset: GooglePreset = {
40 id: GOOGLE_PHOTOS_PRESET_ID,
41 name: "Google Photos",
42 summary: "Albums, uploads, app-created media, and user-selected picker media.",
43 icon: GOOGLE_PHOTOS_ICON,
44 featured: true,
45};
46
36export const googleOpenApiPresets: readonly GoogleOpenApiPreset[] = [47export const googleOpenApiPresets: readonly GoogleOpenApiPreset[] = [
37 {48 {
38 id: "google-calendar",49 id: "google-calendar",
@@ -111,6 +122,22 @@ export const googleOpenApiPresets: readonly GoogleOpenApiPreset[] = [ · 72 unmodified lines
111 icon: "https://fonts.gstatic.com/s/i/productlogos/contacts_2022/v2/192px.svg",122 icon: "https://fonts.gstatic.com/s/i/productlogos/contacts_2022/v2/192px.svg",
112 oauthAudience: "standard-user",123 oauthAudience: "standard-user",
113 },124 },
125 {
126 id: "google-photos-library",
127 name: "Google Photos Library",
128 summary: "Albums, uploads, and app-created media through Google Photos.",
129 url: gd("photoslibrary", "v1"),
130 icon: GOOGLE_PHOTOS_ICON,
131 oauthAudience: "advanced-user",
132 },
133 {
134 id: "google-photos-picker",
135 name: "Google Photos Picker",
136 summary: "Picker sessions and user-selected Google Photos media items.",
137 url: "https://photospicker.googleapis.com/$discovery/rest?version=v1",
138 icon: GOOGLE_PHOTOS_ICON,
139 oauthAudience: "advanced-user",
140 },
114 {141 {
115 id: "google-chat",142 id: "google-chat",
116 name: "Google Chat",143 name: "Google Chat",
@@ -197,6 +224,14 @@ export const googleStandardUserOAuthPresets = googleOpenApiPresets.filter( · 80 unmodified lines
197 (preset) => preset.oauthAudience === "standard-user",224 (preset) => preset.oauthAudience === "standard-user",
198);225);
199 226
227export const googlePhotosPresetIds: readonly string[] = [
228 "google-photos-library",
229 "google-photos-picker",
230];
231
232export const googlePhotosOpenApiPresets: readonly GoogleOpenApiPreset[] =
233 googleOpenApiPresets.filter((preset) => googlePhotosPresetIds.includes(preset.id));
234
200// ---------------------------------------------------------------------------235// ---------------------------------------------------------------------------
201// Representative consent scopes per preset.236// Representative consent scopes per preset.
202//237//
@@ -221,6 +256,11 @@ export const googleOAuthConsentScopes: Readonly<Record<string, readonly string[] · 18 unmodified lines
221 "google-forms": ["https://www.googleapis.com/auth/forms.body"],256 "google-forms": ["https://www.googleapis.com/auth/forms.body"],
222 "google-tasks": ["https://www.googleapis.com/auth/tasks"],257 "google-tasks": ["https://www.googleapis.com/auth/tasks"],
223 "google-people": ["https://www.googleapis.com/auth/contacts"],258 "google-people": ["https://www.googleapis.com/auth/contacts"],
259 "google-photos-library": [
260 "https://www.googleapis.com/auth/photoslibrary.appendonly",
261 "https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata",
262 ],
263 "google-photos-picker": ["https://www.googleapis.com/auth/photospicker.mediaitems.readonly"],
224 "google-chat": ["https://www.googleapis.com/auth/chat.spaces"],264 "google-chat": ["https://www.googleapis.com/auth/chat.spaces"],
225 "google-keep": ["https://www.googleapis.com/auth/keep"],265 "google-keep": ["https://www.googleapis.com/auth/keep"],
226 "google-youtube-data": ["https://www.googleapis.com/auth/youtube"],266 "google-youtube-data": ["https://www.googleapis.com/auth/youtube"],
packages/plugins/openapi/src/sdk/extract.ts+38−5
@@ -316,6 +316,7 @@ export const buildInputSchema = ( · 315 unmodified lines
316): Record<string, unknown> | undefined => {316): Record<string, unknown> | undefined => {
317 const properties: Record<string, unknown> = {};317 const properties: Record<string, unknown> = {};
318 const required: string[] = [];318 const required: string[] = [];
319 let requiredBodyAlternatives: readonly { readonly required: readonly string[] }[] | undefined;
319 320
320 for (const param of parameters) {321 for (const param of parameters) {
321 properties[param.name] = Option.getOrElse(param.schema, () => ({ type: "string" }));322 properties[param.name] = Option.getOrElse(param.schema, () => ({ type: "string" }));
@@ -327,14 +328,45 @@ export const buildInputSchema = ( · 5 unmodified lines
327 if (serverProperty && !("server" in properties)) properties.server = serverProperty;328 if (serverProperty && !("server" in properties)) properties.server = serverProperty;
328 329
329 if (requestBody) {330 if (requestBody) {
330 properties.body = Option.getOrElse(requestBody.schema, () => ({ type: "object" }));
331 if (requestBody.required) required.push("body");
332
333 // When the spec declares multiple media types for this requestBody,331 // When the spec declares multiple media types for this requestBody,
334 // expose `contentType` so the model can pick. Default = first declared.332 // expose `contentType` so the model can pick. Default = first declared.
335 // `body` schema tracks the default; the model is responsible for333 // For mixed bodies, `body` schema tracks the default; the model is
336 // supplying a body shape that matches whichever contentType it picks.334 // responsible for supplying a body shape that matches whichever
335 // contentType it picks. Octet-only operations use `bodyBase64` instead.
337 const contents = Option.getOrUndefined(requestBody.contents);336 const contents = Option.getOrUndefined(requestBody.contents);
337 const defaultIsOctetStream =
338 requestBody.contentType.split(";")[0]?.trim().toLowerCase() === "application/octet-stream";
339 const acceptsOctetStream =
340 defaultIsOctetStream ||
341 contents?.some(
342 (content) =>
343 content.contentType.split(";")[0]?.trim().toLowerCase() === "application/octet-stream",
344 ) === true;
345 const acceptsBody =
346 !defaultIsOctetStream ||
347 contents?.some(
348 (content) =>
349 content.contentType.split(";")[0]?.trim().toLowerCase() !== "application/octet-stream",
350 ) === true;
351 if (acceptsBody) {
352 properties.body = Option.getOrElse(requestBody.schema, () => ({ type: "object" }));
353 }
354 if (acceptsOctetStream) {
355 properties.bodyBase64 = {
356 type: "string",
357 contentEncoding: "base64",
358 contentMediaType: "application/octet-stream",
359 description:
360 "Base64-encoded bytes for application/octet-stream request bodies. When contentType is omitted, this selects application/octet-stream.",
361 };
362 }
363 if (requestBody.required) {
364 if (acceptsOctetStream && acceptsBody) {
365 requiredBodyAlternatives = [{ required: ["body"] }, { required: ["bodyBase64"] }];
366 } else {
367 required.push(acceptsOctetStream ? "bodyBase64" : "body");
368 }
369 }
338 if (contents && contents.length > 1) {370 if (contents && contents.length > 1) {
339 properties.contentType = {371 properties.contentType = {
340 type: "string",372 type: "string",
@@ -352,6 +384,7 @@ export const buildInputSchema = ( · 11 unmodified lines
352 type: "object",384 type: "object",
353 properties,385 properties,
354 ...(required.length > 0 ? { required } : {}),386 ...(required.length > 0 ? { required } : {}),
387 ...(requiredBodyAlternatives ? { anyOf: requiredBodyAlternatives } : {}),
355 additionalProperties: false,388 additionalProperties: false,
356 };389 };
357};390};
packages/plugins/openapi/src/sdk/invoke.ts+120−7
@@ -272,6 +272,28 @@ const bytesFromBase64Prefix = (base64: string): Uint8Array => { · 271 unmodified lines
272const sniffMimeTypeFromBase64 = (base64: string): string | null =>272const sniffMimeTypeFromBase64 = (base64: string): string | null =>
273 sniffMimeType(bytesFromBase64Prefix(base64));273 sniffMimeType(bytesFromBase64Prefix(base64));
274 274
275type DecodedBase64Body = { readonly ok: true; readonly bytes: Uint8Array } | { readonly ok: false };
276
277const base64ToUint8Array = (value: string): Uint8Array | null => {
278 let binary = "";
279 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: atob throws for invalid base64; invalid shapes are treated as non-byte input
280 try {
281 binary = atob(normalizeBase64(value, "base64"));
282 } catch {
283 return null;
284 }
285 const bytes = new Uint8Array(binary.length);
286 for (let index = 0; index < binary.length; index += 1) {
287 bytes[index] = binary.charCodeAt(index);
288 }
289 return bytes;
290};
291
292const decodeBase64Body = (value: string): DecodedBase64Body => {
293 const bytes = base64ToUint8Array(value);
294 return bytes ? { ok: true, bytes } : { ok: false };
295};
296
275const toUint8Array = (value: unknown): Uint8Array | null => {297const toUint8Array = (value: unknown): Uint8Array | null => {
276 if (value instanceof Uint8Array) return value;298 if (value instanceof Uint8Array) return value;
277 if (value instanceof ArrayBuffer) return new Uint8Array(value);299 if (value instanceof ArrayBuffer) return new Uint8Array(value);
@@ -285,6 +307,14 @@ const toUint8Array = (value: unknown): Uint8Array | null => { · 7 unmodified lines
285 return null;307 return null;
286};308};
287 309
310const readNestedBodyBase64 = (value: unknown): unknown =>
311 typeof value === "object" &&
312 value !== null &&
313 !Array.isArray(value) &&
314 Object.prototype.hasOwnProperty.call(value, "bodyBase64")
315 ? (value as Record<string, unknown>).bodyBase64
316 : undefined;
317
288const readHintString = (option: OperationFileHint["dataField"], fallback: string): string =>318const readHintString = (option: OperationFileHint["dataField"], fallback: string): string =>
289 Option.getOrElse(option, () => fallback);319 Option.getOrElse(option, () => fallback);
290 320
@@ -663,18 +693,101 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 372 unmodified lines
663 693
664 if (Option.isSome(operation.requestBody)) {694 if (Option.isSome(operation.requestBody)) {
665 const rb = operation.requestBody.value;695 const rb = operation.requestBody.value;
666 const bodyValue = args.body ?? args.input;696 const contentsOpt = Option.getOrUndefined(rb.contents);
697 const requestedCt = typeof args.contentType === "string" ? args.contentType : undefined;
698 const octetStreamContent = contentsOpt?.find((c) => isOctetStream(c.contentType));
699 const bodyAcceptsOctetStream = Boolean(octetStreamContent) || isOctetStream(rb.contentType);
700 const hasBodyBase64 = Object.prototype.hasOwnProperty.call(args, "bodyBase64");
701 const bodyBase64Raw = args.bodyBase64;
702 const bodyBase64 =
703 typeof bodyBase64Raw === "string" ? decodeBase64Body(bodyBase64Raw) : undefined;
704
705 if (hasBodyBase64 && typeof bodyBase64Raw !== "string") {
706 return yield* new OpenApiInvocationError({
707 message: "`bodyBase64` must be a base64 string",
708 statusCode: Option.none(),
709 });
710 }
711 if (bodyBase64?.ok === false) {
712 return yield* new OpenApiInvocationError({
713 message: "`bodyBase64` is not valid base64",
714 statusCode: Option.none(),
715 });
716 }
717 if (bodyBase64?.ok === true && !bodyAcceptsOctetStream) {
718 return yield* new OpenApiInvocationError({
719 message: "`bodyBase64` requires an application/octet-stream request body",
720 statusCode: Option.none(),
721 });
722 }
723 if (bodyBase64?.ok === true && requestedCt && !isOctetStream(requestedCt)) {
724 return yield* new OpenApiInvocationError({
725 message: "`bodyBase64` requires an application/octet-stream contentType",
726 statusCode: Option.none(),
727 });
728 }
729
730 const rawBodyValue = args.body ?? args.input;
731 const nestedBodyBase64Raw = readNestedBodyBase64(rawBodyValue);
732 const hasNestedBodyBase64 = nestedBodyBase64Raw !== undefined;
733 const nestedBodyBase64 =
734 typeof nestedBodyBase64Raw === "string" ? decodeBase64Body(nestedBodyBase64Raw) : undefined;
735
736 if (hasNestedBodyBase64 && typeof nestedBodyBase64Raw !== "string") {
737 return yield* new OpenApiInvocationError({
738 message: "`body.bodyBase64` must be a base64 string",
739 statusCode: Option.none(),
740 });
741 }
742 if (nestedBodyBase64?.ok === false) {
743 return yield* new OpenApiInvocationError({
744 message: "`body.bodyBase64` is not valid base64",
745 statusCode: Option.none(),
746 });
747 }
748 if (nestedBodyBase64?.ok === true && !bodyAcceptsOctetStream) {
749 return yield* new OpenApiInvocationError({
750 message: "`body.bodyBase64` requires an application/octet-stream request body",
751 statusCode: Option.none(),
752 });
753 }
754 if (nestedBodyBase64?.ok === true && requestedCt && !isOctetStream(requestedCt)) {
755 return yield* new OpenApiInvocationError({
756 message: "`body.bodyBase64` requires an application/octet-stream contentType",
757 statusCode: Option.none(),
758 });
759 }
760
761 const binaryBody = bodyBase64?.ok === true ? bodyBase64 : nestedBodyBase64;
762 const bodyValue = binaryBody?.ok === true ? binaryBody.bytes : rawBodyValue;
763 if (rb.required && bodyValue === undefined) {
764 return yield* new OpenApiInvocationError({
765 message: bodyAcceptsOctetStream
766 ? "Missing required request body: provide `bodyBase64`"
767 : "Missing required request body",
768 statusCode: Option.none(),
769 });
770 }
667 if (bodyValue !== undefined) {771 if (bodyValue !== undefined) {
668 // Resolve which declared media type to use. When the spec declares772 // Resolve which declared media type to use. When the spec declares
669 // multiple, the caller can override via `args.contentType`; otherwise773 // multiple, the caller can override via `args.contentType`; otherwise
670 // we use the first-declared (spec author's preferred ordering).774 // we use the first-declared (spec author's preferred ordering).
671 const contentsOpt = Option.getOrUndefined(rb.contents);
672 const requestedCt = typeof args.contentType === "string" ? args.contentType : undefined;
673 const selected: MediaBinding | undefined =775 const selected: MediaBinding | undefined =
674 contentsOpt && requestedCt776 binaryBody?.ok === true && octetStreamContent
675 ? contentsOpt.find((c) => c.contentType === requestedCt)777 ? octetStreamContent
676 : undefined;778 : contentsOpt && requestedCt
677 const chosenCt = selected?.contentType ?? rb.contentType;779 ? contentsOpt.find((c) => c.contentType === requestedCt)
780 : undefined;
781 const chosenCt =
782 binaryBody?.ok === true && !octetStreamContent && isOctetStream(rb.contentType)
783 ? rb.contentType
784 : (selected?.contentType ?? rb.contentType);
785 if (isOctetStream(chosenCt) && toUint8Array(bodyValue) === null) {
786 return yield* new OpenApiInvocationError({
787 message: "application/octet-stream request body must be bytes; provide `bodyBase64`",
788 statusCode: Option.none(),
789 });
790 }
678 const chosenEncoding = selected791 const chosenEncoding = selected
679 ? Option.getOrUndefined(selected.encoding)792 ? Option.getOrUndefined(selected.encoding)
680 : contentsOpt && contentsOpt[0]793 : contentsOpt && contentsOpt[0]
packages/plugins/openapi/src/sdk/non-json-body.test.ts+249−1
@@ -14,7 +14,7 @@ · 13 unmodified lines
14// ---------------------------------------------------------------------------14// ---------------------------------------------------------------------------
15 15
16import { describe, expect, it } from "@effect/vitest";16import { describe, expect, it } from "@effect/vitest";
17import { Effect, Schema } from "effect";17import { Effect, Exit, Schema } from "effect";
18import { FetchHttpClient, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";18import { FetchHttpClient, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
19import {19import {
20 HttpApi,20 HttpApi,
@@ -259,6 +259,139 @@ describe("OpenAPI non-JSON request body dispatch", () => { · 238 unmodified lines
259 }),259 }),
260 );260 );
261 261
262 it.effect("application/octet-stream: bodyBase64 passes through as bytes", () =>
263 Effect.gen(function* () {
264 const { server, captured } = yield* startEchoServer({
265 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
266 });
267
268 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
269
270 const conn = yield* addOpenApiTestConnection(executor, server, { slug: "bin_b64" });
271
272 yield* executor.execute(conn.address("body.submit"), {
273 bodyBase64: "3q2+7w==",
274 });
275
276 expect(captured.contentType).toBe("application/octet-stream");
277 expect(Array.from(captured.body)).toEqual([0xde, 0xad, 0xbe, 0xef]);
278 }),
279 );
280
281 it.effect("application/octet-stream: invalid bodyBase64 fails before dispatch", () =>
282 Effect.gen(function* () {
283 const { server, captured } = yield* startEchoServer({
284 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
285 });
286
287 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
288
289 const conn = yield* addOpenApiTestConnection(executor, server, { slug: "bin_b64_bad" });
290
291 const exit = yield* executor
292 .execute(conn.address("body.submit"), {
293 bodyBase64: "@@",
294 })
295 .pipe(Effect.exit);
296
297 expect(Exit.isFailure(exit)).toBe(true);
298 expect(captured.contentType).toBe("");
299 expect(captured.body.length).toBe(0);
300 }),
301 );
302
303 it.effect("application/octet-stream: invalid nested bodyBase64 fails before dispatch", () =>
304 Effect.gen(function* () {
305 const { server, captured } = yield* startEchoServer({
306 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
307 });
308
309 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
310
311 const conn = yield* addOpenApiTestConnection(executor, server, {
312 slug: "bin_nested_b64_bad",
313 });
314
315 const exit = yield* executor
316 .execute(conn.address("body.submit"), {
317 body: { bodyBase64: "@@" },
318 })
319 .pipe(Effect.exit);
320
321 expect(Exit.isFailure(exit)).toBe(true);
322 expect(captured.contentType).toBe("");
323 expect(captured.body.length).toBe(0);
324 }),
325 );
326
327 it.effect("application/octet-stream: required body fails before dispatch when missing", () =>
328 Effect.gen(function* () {
329 const { server, captured } = yield* startEchoServer({
330 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
331 });
332
333 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
334
335 const conn = yield* addOpenApiTestConnection(executor, server, {
336 slug: "bin_b64_missing",
337 });
338
339 const exit = yield* executor.execute(conn.address("body.submit"), {}).pipe(Effect.exit);
340
341 expect(Exit.isFailure(exit)).toBe(true);
342 expect(captured.contentType).toBe("");
343 expect(captured.body.length).toBe(0);
344 }),
345 );
346
347 it.effect("application/octet-stream: object body fails before dispatch", () =>
348 Effect.gen(function* () {
349 const { server, captured } = yield* startEchoServer({
350 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
351 });
352
353 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
354
355 const conn = yield* addOpenApiTestConnection(executor, server, {
356 slug: "bin_object_body",
357 });
358
359 const exit = yield* executor
360 .execute(conn.address("body.submit"), {
361 body: { name: "photo.png" },
362 })
363 .pipe(Effect.exit);
364
365 expect(Exit.isFailure(exit)).toBe(true);
366 expect(captured.contentType).toBe("");
367 expect(captured.body.length).toBe(0);
368 }),
369 );
370
371 it.effect("application/octet-stream: string body fails before dispatch", () =>
372 Effect.gen(function* () {
373 const { server, captured } = yield* startEchoServer({
374 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
375 });
376
377 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
378
379 const conn = yield* addOpenApiTestConnection(executor, server, {
380 slug: "bin_string_body",
381 });
382
383 const exit = yield* executor
384 .execute(conn.address("body.submit"), {
385 body: "3q2+7w==",
386 })
387 .pipe(Effect.exit);
388
389 expect(Exit.isFailure(exit)).toBe(true);
390 expect(captured.contentType).toBe("");
391 expect(captured.body.length).toBe(0);
392 }),
393 );
394
262 it.effect(395 it.effect(
263 "format: byte response: Gmail-style image attachment data is exposed as a file artifact",396 "format: byte response: Gmail-style image attachment data is exposed as a file artifact",
264 () =>397 () =>
@@ -754,6 +887,95 @@ describe("OpenAPI non-JSON request body dispatch", () => { · 489 unmodified lines
754 }),887 }),
755 );888 );
756 889
890 it.effect("multi-content: bodyBase64 selects octet-stream without explicit contentType", () =>
891 Effect.gen(function* () {
892 const { server, captured } = yield* startEchoServer({
893 payload: multiContentPayload,
894 transformSpec: replaceRequestBodyContent("/submit", "post", {
895 "application/json": {
896 schema: { type: "object" },
897 },
898 "application/octet-stream": {
899 schema: { type: "string", format: "binary" },
900 },
901 }),
902 });
903
904 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
905
906 const conn = yield* addOpenApiTestConnection(executor, server, { slug: "mc_b64" });
907
908 yield* executor.execute(conn.address("body.submit"), {
909 bodyBase64: "3q2+7w==",
910 });
911
912 expect(captured.contentType).toBe("application/octet-stream");
913 expect(Array.from(captured.body)).toEqual([0xde, 0xad, 0xbe, 0xef]);
914 }),
915 );
916
917 it.effect("multi-content: bodyBase64 rejects a non-octet contentType", () =>
918 Effect.gen(function* () {
919 const { server, captured } = yield* startEchoServer({
920 payload: multiContentPayload,
921 transformSpec: replaceRequestBodyContent("/submit", "post", {
922 "application/json": {
923 schema: { type: "object" },
924 },
925 "application/octet-stream": {
926 schema: { type: "string", format: "binary" },
927 },
928 }),
929 });
930
931 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
932
933 const conn = yield* addOpenApiTestConnection(executor, server, { slug: "mc_b64_bad_ct" });
934
935 const exit = yield* executor
936 .execute(conn.address("body.submit"), {
937 contentType: "application/json",
938 bodyBase64: "3q2+7w==",
939 })
940 .pipe(Effect.exit);
941
942 expect(Exit.isFailure(exit)).toBe(true);
943 expect(captured.contentType).toBe("");
944 expect(captured.body.length).toBe(0);
945 }),
946 );
947
948 it.effect("multi-content: required schema accepts body or bodyBase64", () =>
949 Effect.gen(function* () {
950 const { server } = yield* startEchoServer({
951 payload: multiContentPayload,
952 transformSpec: replaceRequestBodyContent("/submit", "post", {
953 "application/json": {
954 schema: { type: "object" },
955 },
956 "application/octet-stream": {
957 schema: { type: "string", format: "binary" },
958 },
959 }),
960 });
961 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
962
963 const conn = yield* addOpenApiTestConnection(executor, server, { slug: "mc_b64_schema" });
964
965 const view = yield* executor.tools.schema(conn.address("body.submit"));
966 expect(view).not.toBeNull();
967 const schema = view!.inputSchema as {
968 anyOf?: readonly { readonly required?: readonly string[] }[];
969 properties?: {
970 bodyBase64?: { contentEncoding?: string; contentMediaType?: string };
971 };
972 };
973 expect(schema.anyOf).toEqual([{ required: ["body"] }, { required: ["bodyBase64"] }]);
974 expect(schema.properties?.bodyBase64?.contentEncoding).toBe("base64");
975 expect(schema.properties?.bodyBase64?.contentMediaType).toBe("application/octet-stream");
976 }),
977 );
978
757 it.effect("multi-content: tool input schema exposes contentType enum", () =>979 it.effect("multi-content: tool input schema exposes contentType enum", () =>
758 Effect.gen(function* () {980 Effect.gen(function* () {
759 const { server } = yield* startEchoServer({981 const { server } = yield* startEchoServer({
@@ -783,6 +1005,32 @@ describe("OpenAPI non-JSON request body dispatch", () => { · 23 unmodified lines
783 }),1005 }),
784 );1006 );
785 1007
1008 it.effect("octet-stream: tool input schema exposes bodyBase64", () =>
1009 Effect.gen(function* () {
1010 const { server } = yield* startEchoServer({
1011 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
1012 });
1013 const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
1014
1015 const conn = yield* addOpenApiTestConnection(executor, server, { slug: "bin_schema" });
1016
1017 const view = yield* executor.tools.schema(conn.address("body.submit"));
1018 expect(view).not.toBeNull();
1019 const schema = view!.inputSchema as {
1020 required?: string[];
1021 properties?: {
1022 body?: unknown;
1023 bodyBase64?: { contentEncoding?: string; contentMediaType?: string };
1024 };
1025 };
1026 expect(schema.required).toContain("bodyBase64");
1027 expect(schema.required).not.toContain("body");
1028 expect(schema.properties?.body).toBeUndefined();
1029 expect(schema.properties?.bodyBase64?.contentEncoding).toBe("base64");
1030 expect(schema.properties?.bodyBase64?.contentMediaType).toBe("application/octet-stream");
1031 }),
1032 );
1033
786 // -------------------------------------------------------------------------1034 // -------------------------------------------------------------------------
787 // Per-part encoding.contentType in multipart — a metadata field declared1035 // Per-part encoding.contentType in multipart — a metadata field declared
788 // as application/json must ship with its own `Content-Type: application/1036 // as application/json must ship with its own `Content-Type: application/

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t