1.0.1 • Published 3 years ago

ngsameera-cookie v1.0.1

Weekly downloads
6
License
ISC
Repository
-
Last release
3 years ago

NgSSCookie

NgSSCookie used to manage cookies in application which is developed using angular framework.

Installation

  • Add package using npm
    	 ```
    	 npm i ngsameera-cookie --save
    	 ```
    	 or
    	 ```
    	 npm i ngsameera-cookie@latest --save
    	 ```

Documentation

1. Import and inject to file

In this case, used for app.component.ts .

	//app.component.ts
	import { Component } from '@angular/core';

	//import NgSSCookieService
	import { NgSSCookieService } from 'ngsameera-cookie';

	@Component({
		selector: 'app-root',
		template: `<p>ngss-cookie works!</p>`,
		styles: []
		})
		export class AppComponent {
	    /**
	   * Inject NgSSCookieService
	   * @param cookie
	   */
		 constructor(private cookie:NgSSCookieService) { }

	}

2. Save cookie


1. Save as string

Use for save only string value in cookie

  • this.cookie.save.asString(key:string,value:strng,expireDate: Date,path?:string);
  • key argument for key
  • value argument for string value
  • expireDate argument for expire date.
  • pathyou can use fourth argument to set path as string value('/api'). it is optional and default value is /.
//create expire date
var  expireDate:Date = new  Date();
expireDate.setDate(expireDate.getDate()+2);

//save
this.cookie.save.asString('key1','value1',expireDate);

2. Save as DTO

  1. create dto class file. example.dto.ts
export  class Dto{
	name:string;
	age:number;
	married:boolean
	setData(name:string,age:number,married:boolean){
		this.name = name;
		this.age = age;
		this.married = married;
	}
}
  1. Import and implement Use for save dto in cookie. cookie actually saved in cookie as JSON string.
    • this.cookie.save.asDto(key:string,dto:any | any [],expireDate: Date,path?:string);
    • key argument for key
    • dto argument for pass data source to save cookies. It can be object or object array.
    • expireDate argument for expire date.
    • you can use fourth argument to set path as string value('/api'). it is optional and default value is /.
//imort Dto
import { Dto } from './dto';	

//make dto
var  dto:Dto = new Dto();
dto.setData('Suresh',20,false);

//create expire date
var  expireDate:Date = new Date();
expireDate.setDate(expireDate.getDate()+2);

//save dto
this.cookie.save.asDto('key2',dto,expireDate);

3. Read cookie


1. Read as String

Use for read string value or DTO in cookie both. But return only string value.

  • this.cookie.get.asString(key:string);
  • key1 argument for key
//read
var  value: string = this.cookie.get.asString('key1');
console.info(value);

2. Read as DTO

Use for read only DTO cookie.

  • this.cookie.get.asDto(key:string,object:any | any[]);
  • key argument for key
  • dtostore existing cookie in dto. It can be object or object array.
//make new Dto
var  savedDto:Dto = new  Dto();

//read
this.cookie.get.asDto('key2',savedDto);
console.info(savedDto);

4. Update existing cookie


Use save function for update. If existing cookies has path, you should pass path argument(path?:string) to update it . Else path is not essential.

//create expire date

var  expireDate:Date = new  Date();
expireDate.setDate(expireDate.getDate()+2);

//update
this.cookie.save.asString('key1','updated-value1',expireDate);

5. Delete Cookie


Use for delete existing cookie. this.cookie.delete(key:string,path?:string); If existing cookies has path, you should pass path argument(path?:string) to update it . Else path is not essential.

//delete
this.cookie.delete('key1');

Creator

Suresh Sameera