Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 297a772

Browse files
committed
Some improvements
1 parent 7475b20 commit 297a772

File tree

6 files changed

+35
-39
lines changed

6 files changed

+35
-39
lines changed

‎.netlify/functions/products.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎functions/products.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import faunadb from 'faunadb'
2-
31
import { ProductService } from '../lib/product-service.js'
4-
import { client, q,headers } from '../lib/config.js'
2+
import { client, headers } from '../lib/config.js'
53

6-
const service = new ProductService({ client, q })
4+
const service = new ProductService({ client })
75

86
exports.handler = async (event, context) => {
97
console.log('Function `products` invoked')

‎lib/config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ const headers = {
77
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
88
}
99

10-
const q = faunadb.query
11-
1210
const client = new faunadb.Client({
1311
secret: process.env.FAUNADB_SERVER_SECRET,
1412
})
1513

16-
export { q,client, headers }
14+
export { client, headers }

‎lib/product-service.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1+
import faunadb from 'faunadb'
2+
const q = faunadb.query
3+
14
export class ProductService {
25
constructor(data) {
3-
this.q = data.q
46
this.client = data.client
57
}
68

79
async getProducts() {
810
return new Promise((resolve, reject) => {
911
this.client
10-
.query(this.q.Paginate(this.q.Match(this.q.Ref('indexes/all_products'))))
12+
.query(q.Paginate(q.Match(q.Ref('indexes/all_products'))))
1113
.then((response) => {
1214
const productRefs = response.data
15+
const getAllProductDataQuery = q.Map(productRefs, q.Lambda(['ref'], q.Get(q.Var('ref'))))
1316

14-
console.log('Product refs', productRefs)
15-
console.log(`${productRefs.length} todos found`)
16-
17-
const getAllProductDataQuery = productRefs.map((ref) => {
18-
return this.q.Get(ref)
19-
})
20-
// then query the refs
2117
this.client.query(getAllProductDataQuery).then((ret) => {
2218
resolve(ret)
2319
})

‎src/app/app.component.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core'
2+
import { HttpClient } from '@angular/common/http'
23

34
@Component({
4-
selector: 'app-root',
5-
templateUrl: './app.component.html',
6-
styleUrls: ['./app.component.scss']
5+
selector: 'app-root',
6+
templateUrl: './app.component.html',
7+
styleUrls: ['./app.component.scss'],
78
})
8-
export class AppComponent {
9-
title = 'ng-faunadb-netlify-serverless-functions';
9+
export class AppComponent implements OnInit {
10+
title = 'ng-faunadb-netlify-serverless-functions'
11+
12+
constructor(private http: HttpClient) {}
13+
14+
ngOnInit() {
15+
this.http.get('http://localhost:9000/.netlify/functions/products').subscribe((response) => {
16+
console.log('response: ', response)
17+
})
18+
}
1019
}

‎src/app/app.module.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import { BrowserModule } from '@angular/platform-browser';
2-
import { NgModule } from '@angular/core';
1+
import { BrowserModule } from '@angular/platform-browser'
2+
import { NgModule } from '@angular/core'
33

4-
import { AppRoutingModule } from './app-routing.module';
5-
import { AppComponent } from './app.component';
6-
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4+
import { AppRoutingModule } from './app-routing.module'
5+
import { AppComponent } from './app.component'
6+
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
77

8+
import { HttpClientModule } from '@angular/common/http'
89
@NgModule({
9-
declarations: [
10-
AppComponent
11-
],
12-
imports: [
13-
BrowserModule,
14-
AppRoutingModule,
15-
BrowserAnimationsModule
16-
],
17-
providers: [],
18-
bootstrap: [AppComponent]
10+
declarations: [AppComponent],
11+
imports: [BrowserModule, HttpClientModule, AppRoutingModule, BrowserAnimationsModule],
12+
providers: [],
13+
bootstrap: [AppComponent],
1914
})
20-
export class AppModule {}
15+
export class AppModule {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /