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

feat(request): add request provider for SSR without adding express as… #206

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions projects/ngx-cookie-service/express-ssr/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/ngx-cookie-service/express-ssr",
"lib": {
"entryFile": "src/public-api.ts"
}
}
8 changes: 8 additions & 0 deletions projects/ngx-cookie-service/express-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "ngx-cookie-service/express-ssr",
"license": "MIT",
"peerDependencies": {
"@nguniversal/express-engine": "^13.0.1",
"express": "^4.17.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {StaticProvider} from '@angular/core';
import {REQUEST} from '@nguniversal/express-engine/tokens';
import {REQUEST_PROVIDER_TOKEN, RequestProvider} from 'ngx-cookie-service/request-provider';

export const requestProviderFactory = (request: RequestProvider) => {
return request;
};

export const expressRequestProvider: StaticProvider = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

To be honest I have very little knowledge on this. I will read more about this

provide: REQUEST_PROVIDER_TOKEN,
useFactory: requestProviderFactory,
deps: [REQUEST]
};
1 change: 1 addition & 0 deletions projects/ngx-cookie-service/express-ssr/src/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './express-request.provider';
7 changes: 7 additions & 0 deletions projects/ngx-cookie-service/request-provider/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../../dist/ngx-cookie-service/express-ssr",
"lib": {
"entryFile": "src/public-api.ts"
}
}
4 changes: 4 additions & 0 deletions projects/ngx-cookie-service/request-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "ngx-cookie-service/request-provider",
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './request.provider';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {InjectionToken} from '@angular/core';

export interface RequestProvider {
headers? : {
cookie: string;
}
}

export const REQUEST_PROVIDER_TOKEN = new InjectionToken<RequestProvider>('request provider token')
6 changes: 4 additions & 2 deletions projects/ngx-cookie-service/src/lib/cookie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// not use `DOCUMENT` injection and therefore doesn't work well with AoT production builds.
// Package: https://github.com/BCJTI/ng2-cookies

import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import { REQUEST_PROVIDER_TOKEN, RequestProvider } from 'ngx-cookie-service/request-provider';

@Injectable({
providedIn: 'root',
Expand All @@ -14,7 +15,8 @@ export class CookieService {
constructor(
@Inject(DOCUMENT) private document: Document,
// Get the `PLATFORM_ID` so we can check if we're in a browser.
@Inject(PLATFORM_ID) private platformId
@Inject(PLATFORM_ID) private platformId: any,
@Optional() @Inject(REQUEST_PROVIDER_TOKEN) private request: RequestProvider
Copy link
Collaborator

@pavankjadda pavankjadda Jan 13, 2022

Choose a reason for hiding this comment

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

@valorkin I see you created Injection Token and supplied it here. Does it work for CSR along with SSR application? Would you mind creating a sample repo so that we can test this.

@stevermeister take a look at this

Copy link
Owner

Choose a reason for hiding this comment

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

yes, now playing with it here - https://github.com/stevermeister/ssr-coookie

) {
this.documentIsAccessible = isPlatformBrowser(this.platformId);
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"],
"paths": {
"ngx-cookie-service": ["dist/ngx-cookie-service/ngx-cookie-service", "dist/ngx-cookie-service"]
"ngx-cookie-service": ["projects/ngx-cookie-service/src/public-api.ts"],
"ngx-cookie-service/express-ssr": ["projects/ngx-cookie-service/express-ssr/src/public-api.ts"],
"ngx-cookie-service/request-provider": ["projects/ngx-cookie-service/request-provider/src/public-api.ts"],
}
},
"angularCompilerOptions": {
Expand Down