Skip to content

Commit

Permalink
2.0.6 (#67)
Browse files Browse the repository at this point in the history
## 2.0.6

### @craftercms/content

- Add `fetchModelByPath` and `fetchModelByUrl` shortcuts methods
- Improve `parseDescriptor` typings
- Improve `urlTransform` typings

### @craftercms/classes

- Add basic `crafterConf` docs
  • Loading branch information
rart authored Sep 2, 2022
1 parent 223bb59 commit d564a10
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 22 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# SDK Changelog

## 2.0.6

### @craftercms/content

- Add `fetchModelByPath` and `fetchModelByUrl` shortcuts methods
- Improve `parseDescriptor` typings
- Improve `urlTransform` typings

### @craftercms/classes

- Add basic `crafterConf` docs

## 2.0.5

### @craftercms/content
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@craftercms/sdk",
"version": "2.0.5",
"version": "2.0.6",
"private": true,
"workspaces": [
"packages/*"
Expand Down
24 changes: 24 additions & 0 deletions packages/classes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ The examples below assume usage in the style of using via npm. If you're using t
directly importing as a script in the browser, these functions will be under the global variable
named `craftercms.classes` (i.e. `window.craftercms.classes`).

### crafterConf

#### Example

The `crafterConf` is a special class to globally configure CrafterCMS libraries. Most services you can supply these configurations
on a call-by-call basis, but you may simplify by configuring all services via `crafterConf` early-on on your application bootstrap.

```typescript
import { crafterConf } from '@craftercms/classes';

crafterConf.configure({
// Set the base url for all service calls (i.e. urls get built `${baseUrl}/some-endpoint.json`)
baseUrl: process.env.CRAFTER_HOST_NAME,
// Set the site id of the site to fetch from/against
site: process.env.CRAFTER_SITE_NAME,
// Optionally, set cors mode to true/false (default is false)
cors: true,
// Optionally, set any headers you want SDK requests to go out with
headers: {
SOME_HEADER: 'some-value'
}
});
```

### SDK Service

`SDKService` Provides http get and post methods for Crafter services
Expand Down
8 changes: 4 additions & 4 deletions packages/content/src/ContentStoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getItem(path: string, config?: CrafterConfig): Observable<Item>
return SDKService.httpGet(requestURL, { url: path, crafterSite: config.site }, config.headers);
}

export interface GetDescriptorConfig {
export interface GetDescriptorConfig extends CrafterConfig {
flatten: boolean;
}

Expand All @@ -42,10 +42,10 @@ export interface GetDescriptorConfig {
* @param {CrafterConfig & GetDescriptorConfig} config? - The config override options to use
*/
export function getDescriptor(path: string): Observable<Descriptor>;
export function getDescriptor(path: string, config: Partial<CrafterConfig & GetDescriptorConfig>): Observable<Descriptor>;
export function getDescriptor(path: string, config?: Partial<CrafterConfig & GetDescriptorConfig>): Observable<Descriptor> {
export function getDescriptor(path: string, config: Partial<GetDescriptorConfig>): Observable<Descriptor>;
export function getDescriptor(path: string, config?: Partial<GetDescriptorConfig>): Observable<Descriptor> {
let cfg = crafterConf.mix(config);
return SDKService.httpGet(composeUrl(cfg, cfg.endpoints.GET_DESCRIPTOR), {
return SDKService.httpGet<Descriptor>(composeUrl(cfg, cfg.endpoints.GET_DESCRIPTOR), {
url: path,
crafterSite: cfg.site,
flatten: Boolean(config.flatten)
Expand Down
25 changes: 18 additions & 7 deletions packages/content/src/UrlTransformationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

import { Observable } from 'rxjs';

import { crafterConf, SDKService } from '@craftercms/classes';
import { CrafterConfig } from '@craftercms/models';
import { composeUrl } from '@craftercms/utils';
import { Observable } from 'rxjs';

export type UrlTransformers =
| 'storeUrlToRenderUrl'
| 'renderUrlToStoreUrl'
| 'renderUrlToTargetedStoreUrl'
| 'storeUrlToFullRenderUrl'
| 'toWebAppRelativeUrl'
| 'toServletRelativeUrl'
| 'toFullUrl'
| 'toFullHttpsUrl'
| 'folderToIndexUrl'
| 'toTargetedUrl'
| 'toCurrentTargetedUrl';

/**
* Transforms a URL, based on the current site's UrlTransformationEngine.
* @param {string} transformerName - Name of the transformer to apply
* @param {string} url - URL that will be transformed
*/
// TODO: transformerName: 'storeUrlToRenderUrl' | 'renderUrlToStoreUrl' | ???
export function urlTransform(transformerName: string, url: string): Observable<any>;
export function urlTransform(transformerName: string, url: string, config: CrafterConfig): Observable<any>;
export function urlTransform(transformerName: string, url: string, config?: CrafterConfig): Observable<any> {
export function urlTransform(transformerName: UrlTransformers, url: string): Observable<string>;
export function urlTransform(transformerName: UrlTransformers, url: string, config: CrafterConfig): Observable<string>;
export function urlTransform(transformerName: UrlTransformers, url: string, config?: CrafterConfig): Observable<string> {
config = crafterConf.mix(config);
const requestURL = composeUrl(config, config.endpoints.TRANSFORM_URL);
return SDKService.httpGet(requestURL, {
return SDKService.httpGet<string>(requestURL, {
crafterSite: config.site,
transformerName,
url
Expand Down
23 changes: 23 additions & 0 deletions packages/content/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/

import { ContentInstance, DescriptorResponse } from '@craftercms/models';
import { urlTransform } from './UrlTransformationService';
import { getDescriptor, GetDescriptorConfig } from './ContentStoreService';
import { map, switchMap } from 'rxjs/operators';
import { Observable } from 'rxjs';

const systemPropMap = {
guid: 'id',
Expand Down Expand Up @@ -53,6 +57,8 @@ export interface ParseDescriptorOptions {
parseFieldValueTypes: boolean;
}

export function parseDescriptor(data: DescriptorResponse, options?: ParseDescriptorOptions): ContentInstance;
export function parseDescriptor(data: DescriptorResponse[], options?: ParseDescriptorOptions): ContentInstance[];
export function parseDescriptor(data: DescriptorResponse | DescriptorResponse[], options?: ParseDescriptorOptions): ContentInstance | ContentInstance[] {
if (data == null) {
return null;
Expand Down Expand Up @@ -169,6 +175,23 @@ export function parseFieldValue(propName: string, propValue: any): number | stri
}
}

export function fetchModelByPath(path: string): Observable<ContentInstance>;
export function fetchModelByPath(path: string, options?: GetDescriptorConfig & ParseDescriptorOptions): Observable<ContentInstance>;
export function fetchModelByPath(path: string, options?: GetDescriptorConfig & ParseDescriptorOptions): Observable<ContentInstance> {
return getDescriptor(path, { flatten: options?.flatten ?? true }).pipe(
map((descriptor) => parseDescriptor(descriptor, { parseFieldValueTypes: options?.parseFieldValueTypes ?? true }))
);
}

export function fetchModelByUrl(webUrl: string): Observable<ContentInstance>;
export function fetchModelByUrl(webUrl: string, options?: GetDescriptorConfig & ParseDescriptorOptions): Observable<ContentInstance>;
export function fetchModelByUrl(webUrl: string, options?: GetDescriptorConfig & ParseDescriptorOptions): Observable<ContentInstance> {
return urlTransform('renderUrlToStoreUrl', webUrl).pipe(
switchMap((path) => getDescriptor(path as string, { flatten: options?.flatten ?? true })),
map((descriptor) => parseDescriptor(descriptor, { parseFieldValueTypes: options?.parseFieldValueTypes ?? true }))
);
}

/**
* Inspects the data for getItem or getDescriptor responses and returns the inner content object
* */
Expand Down
8 changes: 3 additions & 5 deletions packages/content/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
{
"extends": "../tsconfig-build.json",

"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"paths": {
"rxjs/*": ["../../node_modules/rxjs/*"],
"@craftercms/content": ["."],
"@craftercms/models": ["../../dist/packages-dist/models"],
"@craftercms/utils": ["../../dist/packages-dist/utils"],
"@craftercms/classes": ["../../dist/packages-dist/classes"]
"@craftercms/classes": ["../../dist/packages-dist/classes"],
"rxjs": ["../../node_modules/rxjs"],
"rxjs/*": ["../../node_modules/rxjs/*"]
},
"outDir": "../../dist/packages/content"
},

"files": [
"index.ts"
]

}
4 changes: 3 additions & 1 deletion packages/ice/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"react": ["../../node_modules/react"],
"@craftercms/models": ["../../dist/packages/models"],
"@craftercms/utils": ["../../dist/packages/utils"],
"@craftercms/ice": ["."]
"@craftercms/ice": ["."],
"rxjs": ["../../node_modules/rxjs"],
"rxjs/*": ["../../node_modules/rxjs/*"]
},
"outDir": "../../dist/packages/ice"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/models/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"baseUrl": ".",
"rootDir": ".",
"paths": {
"@craftercms/models": ["."]
"@craftercms/models": ["."],
"rxjs": ["../../node_modules/rxjs"],
"rxjs/*": ["../../node_modules/rxjs/*"]
},
"outDir": "../../dist/packages/models"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/redux/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": "../tsconfig-build.json",

"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"paths": {
"rxjs": ["../../node_modules/rxjs"],
"rxjs/*": ["../../node_modules/rxjs/*"],
"redux": ["../../node_modules/redux"],
"redux-observable": ["../../node_modules/redux-observable"],
Expand All @@ -17,7 +17,6 @@
},
"outDir": "../../dist/packages/redux"
},

"files": [
"index.ts"
]
Expand Down
1 change: 1 addition & 0 deletions packages/search/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"rootDir": ".",
"paths": {
"rxjs": ["../../node_modules/rxjs"],
"rxjs/*": ["../../node_modules/rxjs/*"],
"@craftercms/utils": ["../../dist/packages/utils"],
"@craftercms/models": ["../../dist/packages/models"],
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"baseUrl": ".",
"rootDir": ".",
"paths": {
"@craftercms/utils": ["."]
"@craftercms/utils": ["."],
"rxjs": ["../../node_modules/rxjs"],
"rxjs/*": ["../../node_modules/rxjs/*"]
},
"outDir": "../../dist/packages/utils"
},
Expand Down

0 comments on commit d564a10

Please sign in to comment.