Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-serializer into main
  • Loading branch information
dylanyoung-dev committed Oct 13, 2022
2 parents dad42d6 + 8131bc4 commit c24da54
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 147 deletions.
32 changes: 32 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--- Provide a general summary of your changes in the Title above -->

## Description / Motivation

<!--- Describe your changes in detail -->
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation update (non-breaking change; modified files are limited to the `/docs` directory or other markdown files)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] I have read the Contributing guide.
- [ ] My code/comments/docs fully adhere to the Code of Conduct.
- [ ] My change is a code change.
- [ ] My change is a documentation change and there are NO other updates required.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
![npm](https://img.shields.io/npm/dm/sitecore-cdp-serializer)
[![npm version](https://badge.fury.io/js/sitecore-cdp-serializer.svg)](https://badge.fury.io/js/sitecore-cdp-serializer)

## Installation

Sitecore CDP serializer requires [Node.js](https://nodejs.org) version 14 or above. To install, run the following commands from any directory in your terminal:
Expand Down
39 changes: 34 additions & 5 deletions docs/commands/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,38 @@

The API commands should never be used by a public application. Your Sitecore CDP/Personalize API Username and Password should never be send to anyone you don't trust. It's recommended that you create a Service API account in your Sitecore CDP/Personalize tenant so you can have more control over revoking access in the future.

## Available Commands
## Authentication Commands

| Command | Description | Parameters |
| :---------: | :-------------------------------------------------------------- | :--------------------------------------------- |
| auth | Required command to create access token for future CLI commands | -u, --username, -p, --password, -l, --location |
| connections | Retrieve all connections from your connected tenant | \<none> |
All authentication commands start with `auth`.

| Subcommand | Description | Parameters |
| :--------: | :-------------------------------------------------------------- | :-------------------------------------------------------------------------------------- |
| login | Required command to create access token for future CLI commands | -id, --clientId<br />-s, --clientSecret<br />-l, --location _optional_ (defaults to EU) |
| status | View Authentication/Service Url information | \<none> |
| logout | Logout of the API | \<none> |

### Example

```bash
npx sitecore-cdp-serializer auth login -id {Client Key} -s {API Token} -l {EU|US|APJ}
```

## Template Commands

All template commands start with `templates`.

| Subcommand | Description | Parameters |
| :--------: | :------------------------------------------------------- | :---------------------------------------------------------- |
| get | Gets a list of Templates from the CDP/Personalize tenant | --friendlyId _optional_<br />--templateRef _optional_<br /> |
| create | Creates a New Template based on a JSON object | -t, --template |
| update | Updates an Existing Template based on a JSON object | -t, --template |

### Example

```bash
npx sitecore-cdp-serializer templates get --friendlyId 'sitecore_test_template_1'
```

### Notes

In order to run a templates command, you must have already run an `auth login` command to authenticate to the tenant you wish to access templates from.
19 changes: 19 additions & 0 deletions docs/commands/deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Deploy Commands

## Usage

This command is useful for taking physical files and deploying them to a CDP, Personalize or CDP/Personalize tenant. Details of this command are below:

| Command | Description | Parameters |
| :-----: | :-------------------------------------------------- | :---------------------------------------------------- |
| deploy | Command to take physical files and deploy to Tenant | --artifactPath _optional_ (defaults to `./artifacts`) |

### Example

```bash
npx sitecore-cdp-serializer deploy
```

### Notes

In order to run a deploy command, you must have already run an `auth login` command to authenticate to the tenant you wish to deploy the artifacts out to.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions src/commands/api/Base.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Base service for all API calls
*/

import Configstore from "configstore";
import { AuthToken } from './auth/Auth.interface.js';
import fetch from 'node-fetch';

const BaseService = (config: Configstore) => {
const serviceUrl = config.get('serviceUrl');
const credentials: AuthToken = config.get('credentials');

const Fetch = async(method: string, path: string, body: object | null | undefined) => {
if (!serviceUrl) {
throw 'Service URL not set, re-run auth command';
}

if (!credentials) {
throw 'You must run the auth command first to initialize the CLI';
}

let data = body !== null && body !== undefined
? JSON.stringify(body)
: null;

return await fetch(`https://${serviceUrl}/${path}`, {
method,
body: data,
headers: {
Authorization: `Bearer ${credentials.access_token}`,
'Content-Type': 'application/json',
},
});
}

const Get = async(path: string) => {
return await Fetch("get", path, null);
}

const Post = async(path: string, body: object | null | undefined) => {
return await Fetch("post", path, body);
}

const Put = async(path: string, body: object | null | undefined) => {
return await Fetch("put", path, body);
}

const Delete = async(path: string, body: object | null | undefined) => {
return await Fetch("delete", path, body);
}

return {
Fetch,
Get,
Post,
Put,
Delete,
};
}

export { BaseService };
11 changes: 8 additions & 3 deletions src/commands/api/auth/Auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { AuthToken } from './Auth.interface.js';
import fetch, { Response } from 'node-fetch';
import chalk from 'chalk';
import { Command } from 'commander';
import { initServiceLocation, log, logline } from '../../../utils/index.js';
import {
initServiceLocation,
logline,
logSuccess,
logError,
} from '../../../utils/index.js';

let globalConfig: Configstore;

Expand Down Expand Up @@ -35,10 +40,10 @@ const Authenticate = async (clientId: string, clientSecret: string) => {
if (authToken) {
globalConfig.set('credentials', authToken);

logline(chalk.green('Token Stored for future uses'));
logSuccess('Token Stored for future uses');
}
} else {
logline(chalk.red('Authentication Failed'));
logError('Authentication Failed');
}
};

Expand Down
Loading

0 comments on commit c24da54

Please sign in to comment.