-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make spear to be changeable the api client via plugin parameter. #189
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { DefaultSettings } from "./SettingsInterfaces" | |
import { HTMLElement } from "node-html-parser" | ||
import { FileUtil } from "../utils/file" | ||
import { SpearLog } from "../utils/log" | ||
import { SpearlyJSGenerator } from "@spearly/cms-js-core" | ||
|
||
export type SpearSettings = DefaultSettings | ||
|
||
|
@@ -28,6 +29,7 @@ export interface SpearState { | |
out: { | ||
assetsFiles: AssetFile[] | ||
} | ||
jsGenerator: SpearlyJSGenerator | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. レビュー用コメント: |
||
|
||
export interface SpearOption { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,69 @@ | ||
import { HTMLElement, Node, parse } from 'node-html-parser'; | ||
import { FieldTypeTags, SpearlyApiClient } from '@spearly/sdk-js'; | ||
import getFieldsValuesDefinitions, { generateGetParamsFromAPIOptions, getCustomDateString, ReplaceDefinition } from './Utils.js' | ||
import type { AnalyticsPostParams, Content } from '@spearly/sdk-js' | ||
import type { AnalyticsPostParams, Content, List } from '@spearly/sdk-js' | ||
|
||
/** | ||
* FakeSpearlyApiClient is a fake implementation of SpearlyApiClient. | ||
* We can inject api client for using local file system, like markdown files. | ||
*/ | ||
export interface FakeSpearlyApiClient { | ||
analytics: { | ||
pageView: (params: any) => Promise<void>; | ||
}; | ||
getList(contentTypeId: string, params?: any): Promise<List>; | ||
getContent(contentTypeId: string, contentId: string, params?: any): Promise<Content>; | ||
getContentPreview(contentTypeId: string, contentId: string, previewToken: string): Promise<Content>; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. レビュー用コメント: 他にも フォーム関係などの関数もありますが、Spear ではフォームを利用しないので定義から外しています。 |
||
|
||
export type DateFormatter = (date: Date, dateOnly?: boolean) => string | ||
|
||
export type SpearlyJSGeneratorOption = { | ||
linkBaseUrl: string | undefined; | ||
dateFormatter: DateFormatter | undefined; | ||
} | ||
type SpearlyJSGeneratorInternalOption = { | ||
linkBaseUrl: string; | ||
dateFormatter: DateFormatter; | ||
} | ||
|
||
export type GetContentOption = { | ||
patternName: string, | ||
previewToken?: string, | ||
} | ||
|
||
export type GeneratedContent = { | ||
alias : string, | ||
generatedHtml: string, | ||
tag: string[], | ||
} | ||
|
||
export type GeneratedListContent = { | ||
generatedHtml: string, | ||
tag: string | ||
} | ||
|
||
export type APIOption = Map<string, string | Date | number | string[] | { [key: string]: string | string[] } > | ||
|
||
type SpearlyJSGeneratorInternalOption = { | ||
linkBaseUrl: string; | ||
dateFormatter: DateFormatter; | ||
} | ||
|
||
export class SpearlyJSGenerator { | ||
client: SpearlyApiClient | ||
client: SpearlyApiClient | FakeSpearlyApiClient | ||
options: SpearlyJSGeneratorInternalOption | ||
|
||
constructor(apiKey: string, domain: string, analyticsDomain: string, options: SpearlyJSGeneratorOption | undefined = undefined) { | ||
this.client = new SpearlyApiClient(apiKey, domain, analyticsDomain) | ||
this.options = { | ||
linkBaseUrl: options?.linkBaseUrl || "", | ||
dateFormatter: options?.dateFormatter || function japaneseDateFormatter(date: Date, dateOnly?: boolean) { | ||
dateFormatter: options?.dateFormatter || function japaneseDateFormatter(date: Date, dateOnly?: boolean) { | ||
return getCustomDateString(`YYYY年MM月DD日${!dateOnly ? " hh時mm分ss秒" : ""}`, date) | ||
} | ||
} | ||
} | ||
|
||
injectFakeApiClient(fakeClient: FakeSpearlyApiClient) { | ||
this.client = fakeClient; | ||
} | ||
|
||
convertFromFieldsValueDefinitions(templateHtml: string, replacementArray: ReplaceDefinition[], content: Content, contentType: string): string { | ||
let result = templateHtml | ||
replacementArray.forEach(r => { | ||
|
@@ -60,7 +81,7 @@ export class SpearlyJSGenerator { | |
|
||
const linkMatchResult = result.match(`{%= ${contentType}_#link %}`) | ||
if (!!linkMatchResult && linkMatchResult.length > 0) { | ||
result = result.split(linkMatchResult[0]).join("./" + this.options.linkBaseUrl + "?contentId=" + alias); | ||
result = result.split(linkMatchResult[0]).join("./" + this.options.linkBaseUrl + "?contentId=" + alias); | ||
} | ||
|
||
const aliasMatchResult = result.match(`{%= ${contentType}_#alias %}`) | ||
|
@@ -91,16 +112,16 @@ export class SpearlyJSGenerator { | |
return result | ||
} | ||
|
||
async generateContent(templateHtml: string, contentType: string, contentId: string, option: GetContentOption, insertDebugInfo: boolean): Promise<[html: string, uid:string, patternName: string | null]> { | ||
async generateContent(templateHtml: string, contentType: string, contentId: string, option: GetContentOption, insertDebugInfo: boolean): Promise<[html: string, uid: string, patternName: string | null]> { | ||
try { | ||
const result = option.previewToken | ||
? await this.client.getContentPreview(contentType, contentId, option.previewToken) | ||
: await this.client.getContent(contentType, contentId, | ||
option.patternName | ||
? { | ||
patternName: option.patternName | ||
} | ||
: {} | ||
? { | ||
patternName: option.patternName | ||
} | ||
: {} | ||
); | ||
const replacementArray = getFieldsValuesDefinitions(result.attributes.fields.data, contentType, 2, true, this.options.dateFormatter, insertDebugInfo); | ||
const uid = result.attributes.publicUid; | ||
|
@@ -132,7 +153,7 @@ export class SpearlyJSGenerator { | |
} | ||
if (node.childNodes.length > 0) { | ||
node.childNodes = await this.traverseInjectionSubLoop(node.childNodes as HTMLElement[], apiOptions, insertDebugInfo) | ||
} | ||
} | ||
resultNode.appendChild(node) | ||
} | ||
return resultNode.childNodes | ||
|
@@ -153,7 +174,7 @@ export class SpearlyJSGenerator { | |
const result = await this.client.getList(contentType, generateGetParamsFromAPIOptions(apiOptions)) | ||
let resultHtml = "" | ||
result.data.forEach(c => { | ||
const replacementArray = getFieldsValuesDefinitions(c.attributes.fields.data, variableName || contentType, 2, true, this.options.dateFormatter, insertDebugInfo); | ||
const replacementArray = getFieldsValuesDefinitions(c.attributes.fields.data, variableName || contentType, 2, true, this.options.dateFormatter, insertDebugInfo); | ||
resultHtml += this.convertFromFieldsValueDefinitions(templateHtml, replacementArray, c, contentType) | ||
}) | ||
|
||
|
@@ -190,7 +211,7 @@ export class SpearlyJSGenerator { | |
}) | ||
let resultHtml = "" | ||
targetContents.forEach(c => { | ||
const replacementArray = getFieldsValuesDefinitions(c.attributes.fields.data, variableName || contentType, 2, true, this.options.dateFormatter, insertDebugInfo); | ||
const replacementArray = getFieldsValuesDefinitions(c.attributes.fields.data, variableName || contentType, 2, true, this.options.dateFormatter, insertDebugInfo); | ||
// Special replacement string | ||
replacementArray.push({ | ||
definitionString: `{%= ${contentType}_#tag %}`, | ||
|
@@ -204,12 +225,12 @@ export class SpearlyJSGenerator { | |
}) | ||
}) | ||
return contentsByTag | ||
} catch(e) { | ||
} catch (e) { | ||
return Promise.reject(e) | ||
} | ||
} | ||
|
||
async generateEachContentFromList(templateHtml: string, contentType: string, apiOptions: APIOption, tagFieldName: string, insertDebugInfo: boolean) : Promise<GeneratedContent[]> { | ||
async generateEachContentFromList(templateHtml: string, contentType: string, apiOptions: APIOption, tagFieldName: string, insertDebugInfo: boolean): Promise<GeneratedContent[]> { | ||
try { | ||
const generatedContents: GeneratedContent[] = [] | ||
const result = await this.client.getList(contentType, generateGetParamsFromAPIOptions(apiOptions)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビュー用コメント:
InMemoryMagic
はブラウザ上で Spear を動かすために作成しましたが、 Web Containers をサポートしたことにより不要になります。(#187 のバグで今後削除予定)Comments for Review:
InMemoryMagic
enable running Spear on browser, however this feature might to be unnecessary due to Web Containers (We can drop this feature on #187)