Skip to content

Commit

Permalink
Get PackageDetail from Server
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Aug 19, 2018
1 parent 3d056af commit 0826210
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 83 deletions.
19 changes: 19 additions & 0 deletions server/artifactory-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ async function fetchPackages() {
}

async function getReadme({ scope, packageName }) {
if (process.env.MOCK) {
console.log('bla');
return new Promise((resolve, reject) => {
const converter = new showdown.Converter();
const readme = fs.readFileSync("./server/mock/fractal-menu-enhancer.readme.md");
const html = converter.makeHtml(readme.toString());
resolve({
data: html
});
});
}

const packageDetailResonse = await getPackageDetail({ scope, packageName });
const packageDetail = packageDetailResonse.data;
const latestVersionResponse = await getDistTags({ scope, packageName });
Expand Down Expand Up @@ -67,6 +79,13 @@ async function getReadme({ scope, packageName }) {
}

async function getPackageDetail({ scope, packageName }) {
if (process.env.MOCK) {
return new Promise((resolve, reject) => {
resolve({
data: require(`./mock/fractal-menu-enhancer.json`)
});
});
}
return axios.get(`/${name2url({ scope, packageName })}`);
}

Expand Down
4 changes: 4 additions & 0 deletions server/mock/fractal-menu-enhancer.readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fractal Menu Enhancer
=====================

This is a test.
21 changes: 15 additions & 6 deletions src/model/Package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PackageAbstract } from '@/model/package-abstract';
import {
IAuthor,
IBinMap,
Expand All @@ -11,16 +10,14 @@ import {
IRepository,
IScriptsMap,
} from '@/model/package-json';
import { IDistTags, ITimes } from '@/model/package-abstract';
import { IDistTags, ITimes, IVersions } from '@/model/package-meta-data';
import { PackageMetaDataDTO } from '@/model/package-meta-data';

export default class Package implements PackageMetaDataDTO {
public distTags!: IDistTags;
public time!: ITimes;
public users!: {};
public versions!: {
[key: number]: string;
};
public versions!: IVersions;
// tslint:disable-next-line:variable-name
public _id!: string;
// tslint:disable-next-line:variable-name
Expand Down Expand Up @@ -54,8 +51,9 @@ export default class Package implements PackageMetaDataDTO {
public private?: boolean | undefined;
public publishConfig?: IPublishConfig;
public displayName: any;
private readmeProp: string | null;

constructor(packageDetails: PackageAbstract);
constructor(packageDetails: PackageMetaDataDTO);

constructor(packageMetaData: PackageMetaDataDTO) {
Object.assign(this, packageMetaData);
Expand All @@ -66,6 +64,17 @@ export default class Package implements PackageMetaDataDTO {
: packageMetaData.author;
}
Object.assign(this, { displayName: this.displayName });
this.readmeProp = null;
}

public set readme(readme: string | null) {
if (!this.readmeProp) {
this.readmeProp = readme;
}
}

public get readme(): string | null {
return this.readmeProp;
}

}
9 changes: 0 additions & 9 deletions src/model/PackageDetails.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/model/package-abstract.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/model/package-datails.dto.ts

This file was deleted.

26 changes: 21 additions & 5 deletions src/model/package-meta-data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { PackageAbstract } from '@/model/package-abstract';
import { IPackageJSON } from '@/model/package-json';

export interface PackageMetaDataDTO extends PackageAbstract {
export interface PackageMetaDataDTO extends IPackageJSON {

readonly versions: {
[key: number]: string;
};
readonly distTags: IDistTags;
readonly time: ITimes;
readonly users: {};
readonly _id: string;
readonly _rev: string;
}

export interface ITimes {
[key: number]: Date;
created: string;
modified: string;
}

export interface IDistTags {
[key: string]: number;
}

export interface IVersions {
[key: number]: string | PackageMetaDataDTO;
}
5 changes: 0 additions & 5 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ export default new Router({
name: 'home',
component: Home,
},
{
path: '/about',
name: 'about',
component: About,
},
{
path: '/package/:scope?/:packageName',
name: 'packageDetail',
Expand Down
11 changes: 10 additions & 1 deletion src/services/BackendApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, { AxiosInstance, AxiosPromise } from 'axios';
import { PackagesResponse } from '@/model/PackageResponse';
import Package from '@/model/Package';

export default class BackendApi {

Expand All @@ -20,7 +21,15 @@ export default class BackendApi {
return this.get('packages');
}

private get(route: string): AxiosPromise<PackagesResponse> {
public getPackageDetail({scope, packageName}): AxiosPromise<Package> {
return this.get(`${scope ? `${scope}/` : ''}${packageName}`);
}

public getReadme({scope, packageName}): AxiosPromise<string> {
return this.get(`${scope ? `${scope}/` : ''}${packageName}/readme`);
}

private get(route: string): AxiosPromise<any> {
return this.axios.get(route);
}

Expand Down
22 changes: 0 additions & 22 deletions src/services/PackageDetailService.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/services/PackageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,39 @@ export default class PackagesService {
private packages!: Package[];
private packageNamesList!: string[];
private searchItemList!: SearchItem[];
private packageDetails!: { // TODO: move this caching to backend
[packageName: string]: Package,
};

private constructor() {
this.packages = [];
this.packageNamesList = [];
this.searchItemList = [];
this.packageDetails = {};
}

public async getPackageDetail(packageId: {scope: string, packageName: string}): Promise<Package|string> {
const scope = packageId.scope;
const packageName = packageId.packageName;
const key = scope ? `${scope}/${packageName}` : packageName;
const cachedPackageDetails = scope ? this.packageDetails[key] : this.packageDetails[packageName];
if (cachedPackageDetails) {
return new Promise<Package>((fulfill, reject) => {
fulfill(cachedPackageDetails);
});
}

return new Promise<Package|string>((fulfill, reject) => {
BackendApi.Instance.getPackageDetail({scope, packageName}).then((response) => {
return this.packageDetails[key] = response.data;
}).then((packageDetails) => {
return BackendApi.Instance.getReadme({scope, packageName}).then((response) => {
return this.packageDetails[key].readme = response.data;
});
}).then(() => {
fulfill(this.packageDetails[key]);
});
});
}

public async getPackages(): Promise<Package[]> {
Expand Down
5 changes: 0 additions & 5 deletions src/views/About.vue

This file was deleted.

34 changes: 34 additions & 0 deletions src/views/PackageDetail.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
<template>
<div class="about">
<h2>{{this.$route.params.scope? `${this.$route.params.scope}/` : ''}}{{this.$route.params.packageName}}</h2>
<div>{{data.packageDetail}}</div>
</div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Package from '@/model/Package';
import PackagesService from '@/services/PackageService';
import Router from '@/router';
@Component
export default class PackageDetail extends Vue {
@Prop() private dataProp!: { packageDetail: Package };
private data: { packageDetail: Package | null | string} = this.dataProp;
constructor() {
super();
this.data = {
packageDetail: null,
};
this.getPackageDetails();
}
private getPackageDetails() {
PackagesService.Instance.getPackageDetail({
scope: Router.currentRoute.params.scope,
packageName: Router.currentRoute.params.packageName,
}).then((response) => {
console.log('response', response);
this.data.packageDetail = response;
});
}
}
</script>

0 comments on commit 0826210

Please sign in to comment.