Skip to content

Commit

Permalink
noImplicitAny
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Feb 6, 2024
1 parent 446ad35 commit b4b32f8
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/main/resources/admin/widgets/seo/seo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type {Request} from '/lib/types';


import {
get as getContentByKey,
} from '/lib/xp/content';
Expand Down Expand Up @@ -28,7 +31,7 @@ TODO: Perhaps add (?) icons with info for each data.
TODO: Possibility to click title, desc, image and see the water fall logic and where data is found?
TODO: Grade each data based on amount of text etc. Red, yellow, green. And info about it (best-practise).
*/
export const get = (req) => {
export const get = (req: Request) => {
/*
TODO: Display content settings? If any, then fallbacks.
x": {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/lib/common/commaStringToArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {forceArray} from '@enonic/js-utils/array/forceArray';


export function commaStringToArray(str: CommaSeparatedString): string[] {
const commas = str || '';
const commas: string = str || '';
let arr = commas.split(',');
if (arr) {
arr = arr.map(function (s) { return s.trim() });
arr = arr.map((s) => { return s.trim() });
} else {
arr = forceArray(str); // Make sure we always work with an array
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/resources/lib/common/getBlockRobots.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// import type {Content} from '@enonic-types/lib-content';
import type {Content} from '@enonic-types/lib-content';

import {APP_NAME_PATH, MIXIN_PATH} from '/lib/common/constants';


export const getBlockRobots = (content): boolean => content.x[APP_NAME_PATH]
&& content.x[APP_NAME_PATH][MIXIN_PATH]
&& content.x[APP_NAME_PATH][MIXIN_PATH].blockRobots;
export const getBlockRobots = (content: Content): boolean => !!content.x?.[APP_NAME_PATH]?.[MIXIN_PATH]?.blockRobots;
10 changes: 6 additions & 4 deletions src/main/resources/lib/common/getContentForCanonicalUrl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type {Content} from '@enonic-types/lib-content';


import {get as getContentByKey} from '/lib/xp/content';
import {APP_NAME_PATH, MIXIN_PATH} from '/lib/common/constants';


export const getContentForCanonicalUrl = (content) => content.x[APP_NAME_PATH]
&& content.x[APP_NAME_PATH][MIXIN_PATH]
&& content.x[APP_NAME_PATH][MIXIN_PATH].seoContentForCanonicalUrl
export const getContentForCanonicalUrl = (content: Content): Content =>
content.x?.[APP_NAME_PATH]?.[MIXIN_PATH]?.seoContentForCanonicalUrl
&& getContentByKey({
key: content.x[APP_NAME_PATH][MIXIN_PATH].seoContentForCanonicalUrl
key: content.x[APP_NAME_PATH][MIXIN_PATH].seoContentForCanonicalUrl as string
});
6 changes: 5 additions & 1 deletion src/main/resources/lib/common/getLang.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const getLang = (content, site) => {
import type {Content, Site} from '@enonic-types/lib-content';
import type {MetafieldsSiteConfig} from '/lib/types';


export const getLang = (content: Content, site: Site<MetafieldsSiteConfig>) => {
// Format locale into the ISO format that Open Graph wants.
let locale = 'en_US';
if (content.language || site.language) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/lib/common/getTheConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Site} from '/lib/xp/portal';
import type { MetafieldsSiteConfig } from '../types/MetafieldsSiteConfig';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {getSiteConfig as libPortalGetSiteConfig} from '/lib/xp/portal';
Expand Down Expand Up @@ -30,7 +30,7 @@ export const getTheConfig = ({
if (value === 'true' || value === 'false') {
value = value === 'true';
}
config[prop] = value;
(config as Record<typeof prop, typeof value>)[prop] = value;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/stringOrNull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { isString } from '/lib/common/isString';
* @param o - the value to check
* @returns the string value or null
*/
export function stringOrNull(o) {
export function stringOrNull(o: unknown): string|null {
return isString(o) ? o : null;
}
2 changes: 1 addition & 1 deletion src/main/resources/lib/metadata/getMetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {getPageTitle} from '/lib/common/getPageTitle';
import {getTheConfig} from '/lib/common/getTheConfig';


function _resolveMetadata(params, selfClosingTags=false) {
function _resolveMetadata(params: MetaDataModel, selfClosingTags=false) {
return render(resolve(`metadata${
selfClosingTags ? '-self-closing' : ''
}.html`), params)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/types/MetafieldsSiteConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type {
} from '/lib/types/Content';


export interface MetafieldsSiteConfig {
export declare interface MetafieldsSiteConfig {
blockRobots?: boolean
canonical?: boolean
disableAppConfig?: boolean
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/lib/types/Request.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export declare type Request<
T extends Record<string, string> = {
body?: string
contextPath?: string
cookies?: Record<string, string>
headers?: Record<string, string>
params?: Record<string, string>
pathParams?: Record<string, string>
rawPath?: string
remoteAddress?: string
// webSocket?: unknown
}
> = {
branch: 'draft'|'master'
host: string
method: 'GET'|'POST'|'HEAD'|'PUT'|'DELETE'|'PATCH'
mode: 'edit'|'inline'|'live'|'preview'
path: string
port: string|number
scheme: string
url: string
} & T;
36 changes: 36 additions & 0 deletions src/main/resources/lib/types/Response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// https://developer.enonic.com/docs/xp/stable/framework/http#http-response
export declare interface ComplexCookie {
value: string // Value (required) The value to store in the cookie. This example will create a cookie looking like this complex: value.
path?: string // The paths on the site where this cookie should be available from (and all containing paths). Defaults to empty
domain?: string // Add additional sites that should be able to read the cookie. Defaults to empty (Only the server that creates the cookie can read it.)
comment?: string // A comment describing the cookie. Default to `null. Deprecated and will be removed in future versions of XP.
maxAge?: number // Number of seconds before the browser is allowed to delete the cookie. Defaults to -1 (The cookie will live until the browser is shut down.)
secure?: boolean // Control if the cookie should only be accepted to be created and read over https and similar secure protocols. Defaults to false
httpOnly?: boolean // Control if the cookie is available for scripts or not. If true, only the serverside code can read the cookie. Defaults to false (Also client-side scripts can read the cookie.)
sameSite?: string // XP 7.3.0 SameSite flag for the cookie. Can be lax, strict, none or for "not set". Default is "not set", meaning "browser’s default".
}

export declare interface PageContributions {
headBegin?: string[]
headEnd?: string[]
bodyBegin?: string[]
bodyEnd?: string[]
}

export declare interface Response {
applyFilters?: boolean
body: string|null // Body of the response as string. Null if the response content-type is not of type text.
bodyStream?: unknown
contentType?: string
cookies?: Record<string,string|ComplexCookie>
headers?: Record<string,string>
message?: string
pageContributions?: PageContributions
postProcess?: boolean
redirect?: string
status?: number
webSocket?: {
data: Record<string, unknown>
subProtocols?: string[]
}
}
12 changes: 7 additions & 5 deletions src/main/resources/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type {
MediaImage,
} from '/lib/types/Content';
export type {MetafieldsSiteConfig} from '/lib/types/MetafieldsSiteConfig';
export type {Request} from '/lib/types/Request';
export type {Response} from '/lib/types/Response';


//──────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -133,7 +135,7 @@ const contentValidator = (value: unknown): value is Content => isObject(value)
// && value.hasOwnProperty('data')
// && value.hasOwnProperty('displayName')
// && value.hasOwnProperty('owner')
&& value.hasOwnProperty('type') && isString(value['type'])
&& value.hasOwnProperty('type') && isString((value as Content).type)
// && value.hasOwnProperty('hasChildren') && isBoolean(value['hasChildren'])
// && value.hasOwnProperty('valid') && isBoolean(value['valid'])
// && value.hasOwnProperty('x')
Expand All @@ -156,12 +158,12 @@ export const GraphQLMediaImageBuilder = brand<GraphQLMediaImage>({
const metaFieldsValidator = (value: unknown)/*: value is GraphQLMetaFields*/ => isObject(value)
&& !value.hasOwnProperty('siteName')
? `expected object to have property 'siteName' object:${JSON.stringify(value, null, 4)}`
: !isString(value['siteName'])
? `expected object.siteName to be a string got ${value['siteName']} object:${JSON.stringify(value, null, 4)}`
: !isString((value as GraphQLMetaFields)['siteName'])
? `expected object.siteName to be a string got ${(value as GraphQLMetaFields)['siteName']} object:${JSON.stringify(value, null, 4)}`
: !value.hasOwnProperty('title')
? `expected object to have property 'title' object:${JSON.stringify(value, null, 4)}`
: !isString(value['siteName'])
? `expected object.siteName to be a string got ${value['siteName']} object:${JSON.stringify(value, null, 4)}`
: !isString((value as GraphQLMetaFields)['siteName'])
? `expected object.siteName to be a string got ${(value as GraphQLMetaFields)['siteName']} object:${JSON.stringify(value, null, 4)}`
: true


Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/site/processors/add-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type {Request, Response} from '/lib/types';


import {forceArray} from '@enonic/js-utils/array/forceArray';
import {getFixedHtmlAttrsAsString} from '/lib/metadata/getFixedHtmlAttrsAsString';
import {getMetaData} from '/lib/metadata/getMetaData'
Expand All @@ -9,7 +12,7 @@ const HTML_MEDIA_TYPE = 'text/html';
const XML_MEDIA_TYPES = ['application/xhtml+xml', 'application/xml', 'text/xml'];


export const responseProcessor = (req, res) => {
export const responseProcessor = (req: Request, res: Response) => {
const reusableData = getReusableData({
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
});
Expand Down Expand Up @@ -62,15 +65,15 @@ export const responseProcessor = (req, res) => {
// Push metadata if response content type is html or xml
if ( isResponseContentTypeHtml || isResponseContentTypeXml ) {
const selfClosingTags = isResponseContentTypeXml;
const metadata = getMetaData({
const metadata: string = getMetaData({
applicationConfig: app.config, // NOTE: Using app.config is fine, since it's outside Guillotine Execution Context
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
site,
siteConfig,
content,
returnType: 'html',
selfClosingTags
}) || "";
}) as string || "";
res.pageContributions.headEnd.push(metadata);
}

Expand Down
10 changes: 5 additions & 5 deletions test/lib/metadata/getMetaData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('getMetaData', () => {
}),
siteConfig: metaFieldsSiteConfig,
})).toEqual({
blockRobots: undefined,
blockRobots: false,
canonical: undefined,
canonicalUrl: 'oneContentPathabsolutePageUrl',
description: 'Site description',
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('getMetaData', () => {
site,
siteConfig: metaFieldsSiteConfig,
})).toEqual({
blockRobots: undefined,
blockRobots: false,
canonical: undefined,
canonicalUrl: 'siteContentPathabsolutePageUrl',
description: 'Site description',
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('getMetaData', () => {
site,
siteConfig,
})).toEqual({
blockRobots: undefined,
blockRobots: false,
canonical: undefined,
canonicalUrl: 'oneContentPathabsolutePageUrl',
description: 'Site description',
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('getMetaData', () => {
site,
siteConfig,
})).toEqual({
blockRobots: undefined,
blockRobots: false,
canonical: undefined,
canonicalUrl: 'oneContentPathabsolutePageUrl',
description: 'Site description',
Expand Down Expand Up @@ -305,7 +305,7 @@ describe('getMetaData', () => {
site,
siteConfig,
})).toEqual({
blockRobots: undefined,
blockRobots: false,
canonical: undefined,
canonicalUrl: 'oneContentPathabsolutePageUrl',
description: 'Site description',
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"esModuleInterop": true,
"noImplicitAny": true,
"paths": {
"/lib/xp/*": ["./node_modules/@enonic-types/lib-*"],
"/*": ["./src/main/resources/*"],
},
"skipLibCheck": true,
// "strict": true,
"types": [
"@enonic-types/global"
]
Expand Down

0 comments on commit b4b32f8

Please sign in to comment.