Skip to content

Commit

Permalink
feat: replace resource.getResource with resources.getResources
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlee11 committed Apr 10, 2024
1 parent 8128847 commit d706b0b
Show file tree
Hide file tree
Showing 41 changed files with 511 additions and 507 deletions.
22 changes: 11 additions & 11 deletions src/api/resource.ts → src/api/resources.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
ResourceResponse,
ResourceRequest,
ResourcesResponse,
ResourcesRequest,
ResourceInput,
} from '../types/api/resource';
} from '../types/api/resources';
import {
buildHeaders
} from '../helpers/auth';
import { ResourceMap } from '../types/api/resource/private.types';
import { ResourceMap } from '../types/api/resources/private.types';

export class Resource {
export class Resources {

/**
* Used to load up to 5 resources.
*
* ```ts
* const resourceRequest = {
* const resourcesRequest = {
* 'categoryListResource': {
* type: 'category-list'
* },
Expand Down Expand Up @@ -42,28 +42,28 @@ export class Resource {
* }
* };
* try {
* const resources = await sdk.resource.getResource(resourceRequest);
* const resources = await sdk.resources.getResources(resourcesRequest);
* } catch (error) {
* // Handle errors
* }
* ```
* @throws {@link Error}
*/
async getResource(request: ResourceRequest): Promise<ResourceResponse> {
async getResources(request: ResourcesRequest): Promise<ResourcesResponse> {
const resources: ResourceMap = {};
for (const key in request) {
const resource: ResourceInput = request[key];
resources[key] = resource;
}

const response = await fetch('/s/api/v1/resource', {
const response = await fetch('/s/api/v1/resources', {
method: 'POST',
body: JSON.stringify({
input: resources
}),
headers: buildHeaders()
});
const resourceResponse = await response.json() as ResourceResponse;
return resourceResponse;
const resourcesResponse = await response.json() as ResourcesResponse;
return resourcesResponse;
}
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { Cart } from './api/cart';
import { Orders } from './api/orders';
import { Resource } from './api/resource';
import { Resources } from './api/resources';
import { Places } from './api/places';
import { Template } from './api/template';
import { Location } from './helpers/location';
Expand All @@ -22,7 +22,7 @@ class SiteThemeSDK {
cart: Cart;
orders: Orders;
places: Places;
resource: Resource;
resources: Resources;
template: Template;
customers: Customers;
helpers: {
Expand Down Expand Up @@ -50,7 +50,7 @@ class SiteThemeSDK {
this.cart = new Cart();
this.orders = new Orders(initObj);
this.places = new Places(initObj);
this.resource = new Resource();
this.resources = new Resources();
this.template = new Template();
this.customers = new Customers(initObj);
this.helpers = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

/** ResourceRequest can take up to 5 keys (each representing a resource). */
export interface ResourceRequest {
/** ResourcesRequest can take up to 5 keys (each representing a resource). */
export interface ResourcesRequest {
[key: string]: ResourceInput;
}

/** `key` is the same key we used in the request object, and the value is the resource being returned. */
export interface ResourceResponse {
[key: string]: any;
export interface ResourcesResponse {
[key: string]: {
data?: any;
errors: string[];
};
}

/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
File renamed without changes.
27 changes: 14 additions & 13 deletions test/resource.test.ts → test/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import {
ItemResourceInput,
ResourceRequest
} from '../src/types/api/resource';
ResourcesRequest
} from '../src/types/api/resources';
import { getTestSiteThemeSDK } from './helpers';
import type LooseObject from '../src/types/looseobject';

const STATUS_TEXT = 'status';
const CSRF_TOKEN = 'token';
const CART_TOKEN = 'cart';
const CURRENT_URL = 'currentUrl';
const RESOURCE_API_URL = '/s/api/v1/resource';
const RESOURCES_API_URL = '/s/api/v1/resources';

const okResourceResponse = {
data: {}
const okResourcesResponse = {
data: {},
errors: []
};
const okFetchResponse = createFetchResponse(okResourceResponse, true, 200);
const okFetchResponse = createFetchResponse(okResourcesResponse, true, 200);

function createFetchResponse(data: LooseObject, ok: boolean, status: number, redirectUrl = ''): Response {
return {
Expand Down Expand Up @@ -57,9 +58,9 @@ function createHeadersAndMethodForRequest(method: string = 'POST'): LooseObject
};
}

function createItemResourceRequest(
function createItemResourcesRequest(
{ itemId }: { itemId: string }
): ResourceRequest {
): ResourcesRequest {
const itemInput: ItemResourceInput = {
type: 'item',
filters: {
Expand All @@ -68,7 +69,7 @@ function createItemResourceRequest(
}
};

const request: ResourceRequest = {
const request: ResourcesRequest = {
'item': itemInput
};

Expand Down Expand Up @@ -107,11 +108,11 @@ beforeEach(() => {

describe('Resources request', () => {
it('should make valid fetch', async () => {
const request: ResourceRequest = createItemResourceRequest({ itemId: '1' });
const result = await sdk.resource.getResource(request);
const request: ResourcesRequest = createItemResourcesRequest({ itemId: '1' });
const result = await sdk.resources.getResources(request);

expect(fetch).toHaveBeenCalledWith(
`${RESOURCE_API_URL}`,
`${RESOURCES_API_URL}`,
expect.objectContaining({
...createHeadersAndMethodForRequest(),
body: JSON.stringify({ input: request })
Expand All @@ -120,6 +121,6 @@ describe('Resources request', () => {

expect(fetch).toHaveBeenCalledOnce();

expect(result).toStrictEqual(okResourceResponse);
expect(result).toStrictEqual(okResourcesResponse);
});
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"src/api/customers.ts",
"src/api/orders.ts",
"src/api/places.ts",
"src/api/resource.ts",
"src/api/resources.ts",
"src/api/template.ts",
"src/helpers/item.ts",
"src/helpers/location.ts",
Expand All @@ -33,7 +33,7 @@
"src/types/api/customers/index.ts",
"src/types/api/orders/index.ts",
"src/types/api/places/index.ts",
"src/types/api/resource/index.ts",
"src/types/api/resources/index.ts",
"src/types/api/template/index.ts",
"src/types/helpers/item/index.ts",
"src/types/helpers/location/index.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
[@square/site-theme-sdk](../GettingStarted.md) / [Modules](../modules.md) / [api/resource](../modules/api_resource.md) / Resource
[@square/site-theme-sdk](../GettingStarted.md) / [Modules](../modules.md) / [api/resources](../modules/api_resources.md) / Resources

# Class: Resource
# Class: Resources

[api/resource](../modules/api_resource.md).Resource
[api/resources](../modules/api_resources.md).Resources

## Table of contents

### Constructors

- [constructor](api_resource.Resource.md#constructor)
- [constructor](api_resources.Resources.md#constructor)

### Methods

- [getResource](api_resource.Resource.md#getresource)
- [getResources](api_resources.Resources.md#getresources)

## Constructors

### constructor

**new Resource**()
**new Resources**()

## Methods

### getResource
### getResources

**getResource**(`request`): `Promise`<[`ResourceResponse`](../interfaces/types_api_resource.ResourceResponse.md)\>
**getResources**(`request`): `Promise`<[`ResourcesResponse`](../interfaces/types_api_resources.ResourcesResponse.md)\>

Used to load up to 5 resources.

```ts
const resourceRequest = {
const resourcesRequest = {
'categoryListResource': {
type: 'category-list'
},
Expand Down Expand Up @@ -57,7 +57,7 @@ Used to load up to 5 resources.
}
};
try {
const resources = await sdk.resource.getResource(resourceRequest);
const resources = await sdk.resources.getResources(resourcesRequest);
} catch (error) {
// Handle errors
}
Expand All @@ -67,11 +67,11 @@ Used to load up to 5 resources.

| Name | Type |
| :------ | :------ |
| `request` | [`ResourceRequest`](../interfaces/types_api_resource.ResourceRequest.md) |
| `request` | [`ResourcesRequest`](../interfaces/types_api_resources.ResourcesRequest.md) |

#### Returns

`Promise`<[`ResourceResponse`](../interfaces/types_api_resource.ResourceResponse.md)\>
`Promise`<[`ResourcesResponse`](../interfaces/types_api_resources.ResourcesResponse.md)\>

**`Throws`**

Expand Down
6 changes: 3 additions & 3 deletions typedocs/classes/index.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [cart](index.default.md#cart)
- [orders](index.default.md#orders)
- [places](index.default.md#places)
- [resource](index.default.md#resource)
- [resources](index.default.md#resources)
- [template](index.default.md#template)
- [customers](index.default.md#customers)
- [helpers](index.default.md#helpers)
Expand Down Expand Up @@ -59,9 +59,9 @@ ___

___

### resource
### resources

**resource**: [`Resource`](api_resource.Resource.md)
**resources**: [`Resources`](api_resources.Resources.md)

___

Expand Down
52 changes: 0 additions & 52 deletions typedocs/interfaces/types_api_resource.BaseListResourceInput.md

This file was deleted.

17 changes: 0 additions & 17 deletions typedocs/interfaces/types_api_resource.CartResourceInput.md

This file was deleted.

Loading

0 comments on commit d706b0b

Please sign in to comment.