Skip to content

Commit

Permalink
Merge pull request #99 from HubSpot/port-filetransport
Browse files Browse the repository at this point in the history
Port api/filetransport fix a few API bugs
  • Loading branch information
camden11 authored Feb 8, 2024
2 parents b5a4fbf + f33f725 commit fae3dd4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
18 changes: 16 additions & 2 deletions api/customObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ import { FetchSchemasResponse, Schema } from '../types/Schemas';
const CUSTOM_OBJECTS_API_PATH = 'crm/v3/objects';
const SCHEMA_API_PATH = 'crm-object-schemas/v3/schemas';

type CreateObjectsResponse = {
status: string;
startedAt: string;
completedAt: string;
results: Array<{
id: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
properties: Array<any>;
createdAt: string;
updatedAt: string;
archived: boolean;
}>;
};

export async function batchCreateObjects(
accountId: number,
objectTypeId: string,
objects: JSON
): Promise<void> {
http.post<void>(accountId, {
): Promise<CreateObjectsResponse> {
return http.post<CreateObjectsResponse>(accountId, {
url: `${CUSTOM_OBJECTS_API_PATH}/${objectTypeId}/batch/create`,
data: objects,
});
Expand Down
48 changes: 48 additions & 0 deletions api/fileTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from 'fs';
import path from 'path';
import { getCwd } from '../lib/path';
import http from '../http';

const HUBFILES_API_PATH = '/file-transport/v1/hubfiles';

export async function createSchemaFromHubFile(
accountId: number,
filepath: string
) {
const file = fs.createReadStream(path.resolve(getCwd(), filepath));
return http.post(accountId, {
url: `${HUBFILES_API_PATH}/object-schemas`,
data: {
file,
},
headers: { 'Content-Type': 'multipart/form-data' },
});
}

export async function updateSchemaFromHubFile(
accountId: number,
filepath: string
) {
const file = fs.createReadStream(path.resolve(getCwd(), filepath));
return http.put(accountId, {
url: `${HUBFILES_API_PATH}/object-schemas`,
data: {
file,
},
headers: { 'Content-Type': 'multipart/form-data' },
});
}

export async function fetchHubFileSchema(
accountId: number,
objectName: string,
path: string
) {
return http.getOctetStream(
accountId,
{
url: `${HUBFILES_API_PATH}/object-schemas/${objectName}`,
},
path
);
}
2 changes: 1 addition & 1 deletion http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async function patchRequest<T>(
options: HttpOptions
): Promise<T> {
const configWithAuth = await withAuth(accountId, options);
const { data } = await axios({ ...configWithAuth, method: 'put' });
const { data } = await axios({ ...configWithAuth, method: 'patch' });
return data;
}

Expand Down

0 comments on commit fae3dd4

Please sign in to comment.