Skip to content

Commit

Permalink
✨ Add production endpoint and support switching between PROD and TEST…
Browse files Browse the repository at this point in the history
… endpoints
  • Loading branch information
Patrice Juergens committed Nov 24, 2020
1 parent 6fb916d commit 00eb6bb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
USR=
PWD=
COMPANY=
COMPANY=
ENDPOINT=https://www.zefix.admin.ch/ZefixPublicREST/api/v1
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Thin wrapper around the ZEFIX REST API to search and retrieve data of swiss companies.

**The ZEFIX REST API points to the test system until the final version is released.**

See the public [Swagger file](https://www.zefixintg.admin.ch/ZefixPublicREST/) for the Zefix PublicREST API [here](https://www.zefixintg.admin.ch/ZefixPublicREST/).

## Setup
Expand All @@ -13,8 +11,18 @@ nvm use
npm install
```

Don't forget to update the `.env` file with the needed values properly.
Don't forget to update the `.env` file with the needed values properly:

## Demo
```
USR=<ZEFIX_USER_NAME>
PWD=<ZEFIX_PASSWORD>
COMPANY=<COMPANY_SEARCH_FOR>
ENDPOINT=<TEST_OR_PROD_ENDPOINT_OF_ZEFIX_API>
```

TEST endpoint: https://www.zefixintg.admin.ch/ZefixPublicREST/api/v1
PROD endpoint: https://www.zefix.admin.ch/ZefixPublicREST/api/v1

## Demo
To build the demo, please run `npm run build`.
The demo can be found here: `src/demo.ts` and can simply be run via `npm start`.
6 changes: 5 additions & 1 deletion src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {Zefix} from './index';

const zefix = new Zefix({usr: process.env.USR as string, pwd: process.env.PWD as string});
const zefix = new Zefix({
usr: process.env.USR as string,
pwd: process.env.PWD as string,
endpoint: process.env.ENDPOINT as string
});

try {
zefix
Expand Down
9 changes: 7 additions & 2 deletions src/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ export interface IDetailsResult {
status: 'ACTIVE' | 'CANCELLED' | 'BEING_CANCELLED' | null;
}

export const details = async (uid: string, usr: string, pwd: string): Promise<IDetailsResult | null> => {
const response = await axios.get(`https://www.zefixintg.admin.ch/ZefixPublicREST/api/v1/company/uid/${uid}`, {
export const details = async (
uid: string,
usr: string,
pwd: string,
endpoint: string
): Promise<IDetailsResult | null> => {
const response = await axios.get(`${endpoint}/company/uid/${uid}`, {
headers: {
'Content-Type': 'application/json; charset=utf-8',
Authorization: createBasicAuthHeader(usr, pwd)
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ export {ISearchResult, IDetailsResult};
interface IProps {
usr: string;
pwd: string;
endpoint: string;
}

export class Zefix {
usr;
pwd;
endpoint;

constructor({usr, pwd}: IProps) {
constructor({usr, pwd, endpoint}: IProps) {
this.usr = usr;
this.pwd = pwd;
this.endpoint = endpoint;
}

// Sometimes the UID can be an empty string (whitespaces only), we get rid of this and check the value afterwards
Expand All @@ -26,13 +29,13 @@ export class Zefix {
};

public searchCompany = async (searchTerm: string, activeOnly = false): Promise<ISearchResult[] | null> => {
return search(searchTerm, this.usr, this.pwd, activeOnly);
return search(searchTerm, this.usr, this.pwd, this.endpoint, activeOnly);
};

public getCompanyDetails = async (uid: string): Promise<IDetailsResult | null> => {
if (!this.isValidString(uid)) {
return null;
}
return details(uid, this.usr, this.pwd);
return details(uid, this.usr, this.pwd, this.endpoint);
};
}
3 changes: 2 additions & 1 deletion src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ export const search = async (
term: string,
usr: string,
pwd: string,
endpoint: string,
activeOnly = false
): Promise<ISearchResult[] | null> => {
const response = await axios.post(
'https://www.zefixintg.admin.ch/ZefixPublicREST/api/v1/company/search',
`${endpoint}/company/search`,
{
activeOnly,
name: term
Expand Down

0 comments on commit 00eb6bb

Please # to comment.