customergroups.ts•8.45 kB
import fetch from 'node-fetch';
import { ApiMethodInfo, ApiParameter } from '../api-types.js';
import FormData from 'form-data';
import { baseUrl, apiVersion, getRequestHeaders, handleResponse } from '../config.js';
import * as fs from 'fs';
import * as path from 'path';
import { typeMap } from '../utils/type-map.js';
/**
* Method information for each API endpoint
*/
export const CustomerGroupsMethods: { [key: string]: ApiMethodInfo } = {
list: {
description: "CustomerGroups ListCustomerGroups operation",
method: "get",
path: "/v2/customers/groups",
pathParams: [],
queryParams: [{"name":"cursor","type":"string","description":"A pagination cursor returned by a previous call to this endpoint.\nProvide this cursor to retrieve the next set of results for your original query.\n\nFor more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination)."},{"name":"limit","type":"integer","description":"The maximum number of results to return in a single page. This limit is advisory. The response might contain more or fewer results.\nIf the limit is less than 1 or greater than 50, Square returns a `400 VALUE_TOO_LOW` or `400 VALUE_TOO_HIGH` error. The default value is 50.\n\nFor more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination)."}],
requestType: "ListCustomerGroupsRequest",
isMultipart: false,
originalName: "ListCustomerGroups",
isWrite: false
} as ApiMethodInfo,
create: {
description: "The request must include the `name` value of the group.",
method: "post",
path: "/v2/customers/groups",
pathParams: [],
queryParams: [],
requestType: "CreateCustomerGroupRequest",
isMultipart: false,
originalName: "CreateCustomerGroup",
isWrite: true
} as ApiMethodInfo,
delete: {
description: "CustomerGroups DeleteCustomerGroup operation",
method: "delete",
path: "/v2/customers/groups/{group_id}",
pathParams: [{"name":"group_id","type":"string","description":"The ID of the customer group to delete."}],
queryParams: [],
requestType: "DeleteCustomerGroupRequest",
isMultipart: false,
originalName: "DeleteCustomerGroup",
isWrite: true
} as ApiMethodInfo,
get: {
description: "CustomerGroups RetrieveCustomerGroup operation",
method: "get",
path: "/v2/customers/groups/{group_id}",
pathParams: [{"name":"group_id","type":"string","description":"The ID of the customer group to retrieve."}],
queryParams: [],
requestType: "RetrieveCustomerGroupRequest",
isMultipart: false,
originalName: "RetrieveCustomerGroup",
isWrite: false
} as ApiMethodInfo,
update: {
description: "CustomerGroups UpdateCustomerGroup operation",
method: "put",
path: "/v2/customers/groups/{group_id}",
pathParams: [{"name":"group_id","type":"string","description":"The ID of the customer group to update."}],
queryParams: [],
requestType: "UpdateCustomerGroupRequest",
isMultipart: false,
originalName: "UpdateCustomerGroup",
isWrite: true
} as ApiMethodInfo
};
/**
* Handlers for each API endpoint
*/
export const CustomerGroupsHandlers = {
create: async (accessToken: string, args: Record<string, unknown>) => {
const methodInfo = CustomerGroupsMethods.create;
// Simple endpoint with no path or query parameters
const url = methodInfo.path;
// Make regular JSON request
const response = await fetch(`${baseUrl}${url}`, {
method: methodInfo.method.toUpperCase(),
headers: getRequestHeaders(accessToken),
...(Object.keys(args).length > 0 && ['post', 'put', 'patch'].includes(methodInfo.method.toLowerCase()) && { body: JSON.stringify(args) })
});
return await handleResponse(response)
},
delete: async (accessToken: string, args: Record<string, unknown>) => {
const methodInfo = CustomerGroupsMethods.delete;
// Extract path parameters
const pathParams: Record<string, string> = {};
methodInfo.pathParams.forEach(param => {
const value = args[param.name];
if (value !== undefined) {
pathParams[param.name] = String(value);
delete args[param.name];
} else if (param.required) {
throw new Error(`Missing required path parameter: ${param.name}`);
}
});
// Build URL with path parameters
let url = methodInfo.path;
// Replace path parameters
Object.entries(pathParams).forEach(([key, value]) => {
url = url.replace(`{${key}}`, encodeURIComponent(value));
});
// Make regular JSON request
const response = await fetch(`${baseUrl}${url}`, {
method: methodInfo.method.toUpperCase(),
headers: getRequestHeaders(accessToken),
...(Object.keys(args).length > 0 && ['post', 'put', 'patch'].includes(methodInfo.method.toLowerCase()) && { body: JSON.stringify(args) })
});
return await handleResponse(response)
},
get: async (accessToken: string, args: Record<string, unknown>) => {
const methodInfo = CustomerGroupsMethods.get;
// Extract path parameters
const pathParams: Record<string, string> = {};
methodInfo.pathParams.forEach(param => {
const value = args[param.name];
if (value !== undefined) {
pathParams[param.name] = String(value);
delete args[param.name];
} else if (param.required) {
throw new Error(`Missing required path parameter: ${param.name}`);
}
});
// Build URL with path parameters
let url = methodInfo.path;
// Replace path parameters
Object.entries(pathParams).forEach(([key, value]) => {
url = url.replace(`{${key}}`, encodeURIComponent(value));
});
// Make regular JSON request
const response = await fetch(`${baseUrl}${url}`, {
method: methodInfo.method.toUpperCase(),
headers: getRequestHeaders(accessToken),
...(Object.keys(args).length > 0 && ['post', 'put', 'patch'].includes(methodInfo.method.toLowerCase()) && { body: JSON.stringify(args) })
});
return await handleResponse(response)
},
update: async (accessToken: string, args: Record<string, unknown>) => {
const methodInfo = CustomerGroupsMethods.update;
// Extract path parameters
const pathParams: Record<string, string> = {};
methodInfo.pathParams.forEach(param => {
const value = args[param.name];
if (value !== undefined) {
pathParams[param.name] = String(value);
delete args[param.name];
} else if (param.required) {
throw new Error(`Missing required path parameter: ${param.name}`);
}
});
// Build URL with path parameters
let url = methodInfo.path;
// Replace path parameters
Object.entries(pathParams).forEach(([key, value]) => {
url = url.replace(`{${key}}`, encodeURIComponent(value));
});
// Make regular JSON request
const response = await fetch(`${baseUrl}${url}`, {
method: methodInfo.method.toUpperCase(),
headers: getRequestHeaders(accessToken),
...(Object.keys(args).length > 0 && ['post', 'put', 'patch'].includes(methodInfo.method.toLowerCase()) && { body: JSON.stringify(args) })
});
return await handleResponse(response)
},
list: async (accessToken: string, args: Record<string, unknown>) => {
const methodInfo = CustomerGroupsMethods.list;
// Extract query parameters
const queryParams: Record<string, string> = {};
methodInfo.queryParams.forEach(param => {
const value = args[param.name];
if (value !== undefined) {
queryParams[param.name] = String(value);
delete args[param.name];
}
});
// Build URL with query parameters
let url = methodInfo.path;
// Add query parameters
const queryString = Object.entries(queryParams)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
if (queryString) {
url = `${url}?${queryString}`;
}
// Make regular JSON request
const response = await fetch(`${baseUrl}${url}`, {
method: methodInfo.method.toUpperCase(),
headers: getRequestHeaders(accessToken),
...(Object.keys(args).length > 0 && ['post', 'put', 'patch'].includes(methodInfo.method.toLowerCase()) && { body: JSON.stringify(args) })
});
return await handleResponse(response)
}
};