Skip to content

Commit

Permalink
Merge branch 'main' into SITES-26591-chat-completions2
Browse files Browse the repository at this point in the history
  • Loading branch information
atopper committed Nov 12, 2024
2 parents 32ec468 + 36d9323 commit ef76a77
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 2,837 deletions.
2,980 changes: 147 additions & 2,833 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/spacecat-shared-content-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [@adobe/spacecat-shared-content-client-v1.2.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-content-client-v1.1.22...@adobe/spacecat-shared-content-client-v1.2.0) (2024-11-12)


### Features

* create content client directly from domain ([17aec10](https://github.com/adobe/spacecat-shared/commit/17aec10cabb7b50d35588054587a96032b52ef73))

# [@adobe/spacecat-shared-content-client-v1.1.22](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-content-client-v1.1.21...@adobe/spacecat-shared-content-client-v1.1.22) (2024-11-11)


Expand Down
21 changes: 20 additions & 1 deletion packages/spacecat-shared-content-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ import { ContentClient } from '../src/index.js';

const context = {}; // Your AWS Lambda context object
const gdriveclient = await ContentClient.createFrom(context, { url: 'GOOGLE_DRIVE_URL', type: 'drive.google' });
const results = await client.getPageMetadata('/path1');
console.log(results);
```
```js
import { ContentClient } from '../src/index.js';

const env = {}; // Your env variables to connect to spacecat apis and google drive
const onedriveclient = await ContentClient.createFromDomain('example.com', env, log);

const results = await client.getPageMetadata('/path1');
console.log(results);
```
Expand All @@ -38,9 +47,19 @@ console.log(results);
```js
import { ContentClient } from '../src/index.js';

const context = {}; // Your AWS Lambda context object
const context = {}; // Your env variables to connect to spacecat apis and onedrive
const onedriveclient = await ContentClient.createFrom(context, { url: 'ONEDRIVE_URL', type: 'onedrive' });

const results = await client.getPageMetadata('/path1');
console.log(results);
```

```js
import { ContentClient } from '../src/index.js';

const env = {}; // Your AWS Lambda context object
const onedriveclient = await ContentClient.createFromDomain('example.com', env, log);

const results = await client.getPageMetadata('/path1');
console.log(results);
```
Expand Down
4 changes: 2 additions & 2 deletions packages/spacecat-shared-content-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-shared-content-client",
"version": "1.1.22",
"version": "1.2.0",
"description": "Shared modules of the Spacecat Services - Content Client",
"type": "module",
"engines": {
Expand Down Expand Up @@ -35,12 +35,12 @@
},
"dependencies": {
"@adobe/helix-universal": "5.0.6",
"@adobe/spacecat-shared-data-access": "1.50.0",
"@adobe/spacecat-helix-content-sdk": "1.2.7",
"@adobe/spacecat-shared-utils": "1.22.4",
"graph-data-structure": "4.1.0"
},
"devDependencies": {
"@adobe/spacecat-shared-data-access": "1.50.0",
"chai": "5.1.2",
"chai-as-promised": "8.0.0",
"esmock": "2.6.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
*/

import { createFrom as createContentSDKClient } from '@adobe/spacecat-helix-content-sdk';
import { hasText, isObject } from '@adobe/spacecat-shared-utils';
import {
composeBaseURL, hasText, isObject, tracingFetch,
} from '@adobe/spacecat-shared-utils';
import { Graph, hasCycle } from 'graph-data-structure';
import { SiteDto } from '@adobe/spacecat-shared-data-access/src/dto/site.js';

const CONTENT_SOURCE_TYPE_DRIVE_GOOGLE = 'drive.google';
const CONTENT_SOURCE_TYPE_ONEDRIVE = 'onedrive';
Expand Down Expand Up @@ -193,6 +196,31 @@ export default class ContentClient {
return new ContentClient(config, site, log);
}

static async createFromDomain(domain, env, log = console) {
const baseUrl = composeBaseURL(domain);
const siteBaseUrlEncoded = Buffer.from(baseUrl).toString('base64');
let site;
const sitesApiEndpoint = `${env.SPACECAT_API_ENDPOINT}/sites/by-base-url`;
try {
const response = await tracingFetch(`${sitesApiEndpoint}/${siteBaseUrlEncoded}`, {
method: 'GET',
headers: {
'x-api-key': env.SPACECAT_API_KEY,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`Failed to fetch ${domain}`);
}
site = await response.json();
const siteDto = SiteDto.fromDynamoItem(site);
return ContentClient.createFrom({ log, env }, siteDto);
} catch (e) {
log.error(`Failed to fetch ${domain}: ${e.message}`);
throw new Error(`Failed to fetch ${domain}`);
}
}

constructor(config, site, log) {
validateSite(site);
validateConfiguration(config, site.getHlxConfig()?.content.source?.type);
Expand Down
12 changes: 12 additions & 0 deletions packages/spacecat-shared-content-client/src/clients/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ export class ContentClient {
*/
static createFrom(context: UniversalContext, site: object): ContentClient;

/**
* Creates a new ContentClient instance from the given domain and environment.
*
* @param {string} domain The domain of the site to create the ContentClient for.
* @param {Object} env The environment object that contains the required configuration
* for the site's content source type.
* @returns {Promise<ContentClient>} A promise that resolves to the ContentClient instance.
* @throws {Error} If the domain is not a string or empty.
* @throws {Error} If the env is not an object or does not contain the required configuration.
*/
static createFromDomain(domain: string, env: object,): Promise<ContentClient>;

/**
* Returns the metadata for the given page path. The document backing the path
* is resolved and the metadata is extracted from it. The metadata is a Map where the entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js';
import { Mdast } from '@adobe/spacecat-helix-content-sdk/src/mdast.js';
import nock from 'nock';

use(chaiAsPromised);
use(sinonChai);
Expand Down Expand Up @@ -198,6 +199,7 @@ describe('ContentClient', () => {
ONEDRIVE_AUTHORITY: 'https://authority.uri',
ONEDRIVE_CLIENT_ID: 'onedrive-client-id',
ONEDRIVE_CLIENT_SECRET: 'onedrive-client-secret',
SPACECAT_API_ENDPOINT: 'https://spacecat.experiencecloud.live/api/v1',
};
context = { env, log };

Expand All @@ -215,6 +217,66 @@ describe('ContentClient', () => {
});
});

describe('createFromDomain', () => {
it('should create a ContentClient instance from a domain', async () => {
const domain = 'example.com';
const encodedBaseURL = 'aHR0cHM6Ly9leGFtcGxlLmNvbQ==';
const site = {
id: 'test-site',
baseURL: 'https://example.com',
hlxConfig: { content: { source: { type: 'drive.google' } } },
};
nock(env.SPACECAT_API_ENDPOINT)
.get(`/sites/by-base-url/${encodedBaseURL}`)
.reply(200, site);

const client = await ContentClient.createFromDomain(
domain,
env,
log = { info: sinon.spy(), error: sinon.spy() },
);
expect(client).to.be.an.instanceof(ContentClient);
});

it('should throw an error if site is not fetched', async () => {
const domain = 'example.com';
const encodedBaseURL = 'aHR0cHM6Ly9leGFtcGxlLmNvbQ==';
nock(env.SPACECAT_API_ENDPOINT)
.get(`/sites/by-base-url/${encodedBaseURL}`)
.reply(404, null);

try {
await ContentClient.createFromDomain(
domain,
env,
log = { info: sinon.spy(), error: sinon.spy() },
);
} catch (error) {
expect(error.message).to.equal(`Failed to fetch ${domain}`);
expect(log.error.calledOnce).to.be.true;
}
});

it('should log and throw an error if fetch fails', async () => {
const domain = 'example.com';
const encodedBaseURL = 'aHR0cHM6Ly9leGFtcGxlLmNvbQ==';
nock(env.SPACECAT_API_ENDPOINT)
.get(`/sites/by-base-url/${encodedBaseURL}`)
.replyWithError(200, 'Network error');

try {
await ContentClient.createFromDomain(
domain,
env,
log = { info: sinon.spy(), error: sinon.spy() },
);
throw new Error('Expected error was not thrown');
} catch (error) {
expect(error.message).to.equal(`Failed to fetch ${domain}`);
}
});
});

describe('constructor', () => {
it('validates and sets config, site, and rawClient for Google Drive', () => {
const client = ContentClient.createFrom(context, siteConfigGoogleDrive);
Expand Down

0 comments on commit ef76a77

Please sign in to comment.