Skip to content
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

Add Request and Response to lib-portal #9742 #9745

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

edloidas
Copy link
Member

After discussion with @rymsha, it was decided, that Request and Response types will go to lib-portal, because it's where they actually belong in Java as part of the portal-api.

@codecov
Copy link

codecov bot commented Sep 27, 2022

Codecov Report

Base: 84.11% // Head: 84.11% // Decreases project coverage by -0.00% ⚠️

Coverage data is based on head (021f1f6) compared to base (3a8a8fc).
Patch has no changes to coverable lines.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #9745      +/-   ##
============================================
- Coverage     84.11%   84.11%   -0.01%     
+ Complexity    19418    19417       -1     
============================================
  Files          2595     2595              
  Lines         68699    68699              
  Branches       5540     5540              
============================================
- Hits          57787    57785       -2     
- Misses         8151     8152       +1     
- Partials       2761     2762       +1     
Impacted Files Coverage Δ
...xp/repo/impl/branch/storage/BranchServiceImpl.java 88.73% <0.00%> (-1.41%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@tajakobsen tajakobsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might not be a good idea to add type parameters for Params, Cookies and Headers. The reason is that we usually can't use that same interface in both ends of the "communication", and we don't really know what shape will come in on these parameters, because the initiator of the call can really pass in anything.

modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts Outdated Show resolved Hide resolved
sameSite?: 'lax' | 'strict' | 'none' | ''; // ''
}

interface Request<Params extends Record<string, string | undefined> = Record<string, string | undefined>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a url with the same query parameter listed more then once, the value of the param in XP is an Array<string>

Suggested change
interface Request<Params extends Record<string, string | undefined> = Record<string, string | undefined>,
interface Request<Params extends Record<string, string | Array<string> | undefined> = Record<string, string | Array<string> | undefined>,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to not pass in these type parameters, because your get or post function has to be able to handle any input, both missing and string and Arrays, so there is little value in locking in a shape.

The value of specifying these shapes in type parameters, would be if you could use the same shape "in both ends". That means that you specify an interface that can be used both by Request and serviceUrl() or pageUrl().

modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts Outdated Show resolved Hide resolved
modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts Outdated Show resolved Hide resolved
modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts Outdated Show resolved Hide resolved
modules/lib/lib-portal/src/main/resources/lib/xp/portal.ts Outdated Show resolved Hide resolved
contentType: string;
headers: Record<string, string | undefined>;
cookies: Record<string, string | Cookie | undefined>;
redirect: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
redirect: string;
redirect?: string;

redirect is a "write only" property, initially it is undefined

bodyBegin?: string | string[];
bodyEnd?: string | string[];
};
applyFilters: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edloidas edloidas marked this pull request as draft September 29, 2022 14:11
Copy link
Contributor

@tajakobsen tajakobsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few comments here. :) Don't know if I'm coming with this feedback prematurely.

}

interface AssetUrlHandler {
createUrl(value: object): string;
createUrl(value: ScriptValue): string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this depending on the global ScriptValue to exist? Because I had problems using nodeLib because of that. Can ScriptValue be imported from core or similar?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't global always exist?
__ is always there, so does __.toScriptValue. same as require

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not during runtime, unless global is included as dependency.
Right, I'd better revert it back to object. Totally forgot about this problem.


export interface Request<Params extends RequestParams = RequestParams,
Headers extends Record<string, string | undefined> = Record<string, string | undefined>,
Cookies extends Record<string, string | Cookie | undefined> = Record<string, string | Cookie | undefined>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct that a cookie can be string | Cookie | undefined? Since this comes from XP, it seems like a risky thing to get a cookie be both string and Cookie?

Could it be that cookies in Response has this shape, but not in Request?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request cookies are always string. (except for the cases when there is no such cookie)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, let's make it string/undefined then. But the Response cookie can still be a string/Cookie/undefined, right?

contentType?: string;
}

export interface Response<ResponseBody = unknown> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every field in Response should be optional, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a dilemma here. Response that controllers receive has many non-optional fields. But the one that controllers could return may have all fields optional (because it is sort-of patch to existing one)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably necessary to have two Response types. Maybe something like this:

export interface InputResponse { // sorry about the bad naming here
  status: number;
  ...
}

export type Response = Partial<InputResponse>;

Be careful about naming it Response or Request, I think there will normally be a naming collision against the global fields in the web platform. (That was why I namespaced it in enonic-types)

@@ -241,20 +296,20 @@ interface PageUrlHandler {
*
* @returns {string} The generated URL.
*/
export function pageUrl(params: PageUrlParams): string {
export function pageUrl<Params extends RequestParams = RequestParams>(params: PageUrlParams<Params>): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all of these functions (pageUrl, serviceUrl) both number and boolean is valid types of the value (and object are just url-encoded, but that might be more coincidental).

Should maybe Params not extend RequestParams but another shape instead that allows for other types of the values.


export type RequestParams = Record<string, string | string[] | undefined>;

export interface Request<Params extends RequestParams = RequestParams,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider removing (all or some of) the type parameters like I suggested earlier?

Headers extends Record<string, string | undefined> = Record<string, string | undefined>,
Cookies extends Record<string, string | Cookie | undefined> = Record<string, string | Cookie | undefined>,
> {
method: LiteralUnion<'GET' | 'PUT' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'OPTIONS' | 'TRACE' | 'CONNECT'>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a list of HTTP verbs you can use in the Java-part of the system:

GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants