1. शुरू करने से पहले
Firebase एक्सटेंशन, एचटीटीपी अनुरोधों या Firebase और Google के अन्य प्रॉडक्ट से ट्रिगर होने वाले इवेंट के जवाब में कोई खास टास्क या टास्क का सेट पूरा करता है. जैसे, Firebase क्लाउड से मैसेज, Cloud Firestore या Pub/Sub.
आपको क्या बनाने को मिलेगा
इस कोडलैब में, जियोहैशिंग के लिए Firebase एक्सटेंशन बनाया जाएगा. डिप्लॉय होने के बाद, आपका एक्सटेंशन X- और Y-निर्देशांकों को जियोहैश में बदल देता है. ऐसा Firestore इवेंट के जवाब में या कॉल किए जा सकने वाले फ़ंक्शन के इनवोकेशन के ज़रिए किया जाता है. इसका इस्तेमाल, डेटा को सेव करने के लिए सभी टारगेट प्लैटफ़ॉर्म पर geofire लाइब्रेरी लागू करने के विकल्प के तौर पर किया जा सकता है. इससे आपका समय बचता है.

आपको क्या सीखने को मिलेगा
- Cloud Functions के मौजूदा कोड को डिस्ट्रिब्यूट किए जा सकने वाले Firebase एक्सटेंशन में बदलने का तरीका
extension.yamlफ़ाइल सेट अप करने का तरीका- एक्सटेंशन में संवेदनशील स्ट्रिंग (एपीआई कुंजियां) सेव करने का तरीका
- एक्सटेंशन के डेवलपर को, अपनी ज़रूरतों के हिसाब से इसे कॉन्फ़िगर करने की अनुमति कैसे दें
- एक्सटेंशन की जांच करने और उसे लागू करने का तरीका
आपको इन चीज़ों की ज़रूरत होगी
- Firebase CLI (इंस्टॉल और लॉगिन करें)
- Google खाता, जैसे कि Gmail खाता
- Node.js और
npm - आपका पसंदीदा डेवलपमेंट एनवायरमेंट
2. सेट अप करना
कोड पाएं
इस एक्सटेंशन के लिए ज़रूरी सभी चीज़ें, GitHub रिपो में मौजूद हैं. शुरू करने के लिए, कोड को कॉपी करें और इसे अपने पसंदीदा डेवलपमेंट एनवायरमेंट में खोलें.
- डाउनलोड की गई ज़िप फ़ाइल को अनपैक करें.
- ज़रूरी डिपेंडेंसी इंस्टॉल करने के लिए,
functionsडायरेक्ट्री में टर्मिनल खोलें औरnpm installकमांड चलाएं.
Firebase सेट अप करना
इस कोडलैब में, Firebase Emulator का इस्तेमाल करने का सुझाव दिया गया है. अगर आपको किसी असली Firebase प्रोजेक्ट के साथ एक्सटेंशन डेवलपमेंट आज़माना है, तो Firebase प्रोजेक्ट बनाएं लेख पढ़ें. इस कोडलैब में Cloud Functions का इस्तेमाल किया गया है. इसलिए, अगर आपको एम्युलेटर के बजाय किसी असली Firebase प्रोजेक्ट का इस्तेमाल करना है, तो आपको ब्लेज़ प्राइसिंग प्लान पर अपग्रेड करना होगा.
क्या आपको किसी यूनिट को स्किप करके आगे बढ़ना है?
कोडलैब का पूरा वर्शन डाउनलोड किया जा सकता है. अगर आपको कोई समस्या आती है या आपको यह देखना है कि पूरा हो चुका एक्सटेंशन कैसा दिखता है, तो GitHub रिपॉज़िटरी की codelab-end ब्रांच देखें या पूरा हो चुका ज़िप डाउनलोड करें.
3. कोड की समीक्षा करना
- ज़िप फ़ाइल से
index.tsफ़ाइल खोलें. ध्यान दें कि इसमें दो Cloud Functions के एलान शामिल हैं.
इन फ़ंक्शन का क्या काम है?
इन डेमो फ़ंक्शन का इस्तेमाल, जियोहैशिंग के लिए किया जाता है. ये फ़ंक्शन, निर्देशांकों के जोड़े को लेते हैं और उन्हें ऐसे फ़ॉर्मैट में बदलते हैं जो Firestore में जियो क्वेरी के लिए ऑप्टिमाइज़ किया गया हो. ये फ़ंक्शन, एपीआई कॉल के इस्तेमाल का सिम्युलेशन करते हैं. इससे आपको एक्सटेंशन में संवेदनशील डेटा टाइप को मैनेज करने के बारे में ज़्यादा जानकारी मिलती है. ज़्यादा जानकारी के लिए, Firestore में मौजूद डेटा पर जियो क्वेरी चलाने से जुड़ा दस्तावेज़ देखें.
फ़ंक्शन कॉन्स्टेंट
कॉन्स्टेंट का एलान, index.ts फ़ाइल में सबसे ऊपर किया जाता है. इनमें से कुछ कॉन्स्टेंट को एक्सटेंशन के तय किए गए ट्रिगर में रेफ़र किया जाता है.
index.ts
import {firestore} from "firebase-functions";
import {initializeApp} from "firebase-admin/app";
import {GeoHashService, ResultStatusCode} from "./fake-geohash-service";
import {onCall} from "firebase-functions/v1/https";
import {fieldValueExists} from "./utils";
const documentPath = "users/{uid}";
const xField = "xv";
const yField = "yv";
const apiKey = "1234567890";
const outputField = "hash";
initializeApp();
const service = new GeoHashService(apiKey);
Firestore ट्रिगर
index.ts फ़ाइल में पहला फ़ंक्शन ऐसा दिखता है:
index.ts
export const locationUpdate = firestore.document(documentPath)
.onWrite((change) => {
// item deleted
if (change.after == null) {
return 0;
}
// double check that both values exist for computation
if (
!fieldValueExists(change.after.data(), xField) ||
!fieldValueExists(change.after.data(), yField)
) {
return 0;
}
const x: number = change.after.data()![xField];
const y: number = change.after.data()![yField];
const hash = service.convertToHash(x, y);
// This is to check whether the hash value has changed. If
// it hasn't, you don't want to write to the document again as it
// would create a recursive write loop.
if (fieldValueExists(change.after.data(), outputField)
&& change.after.data()![outputField] == hash) {
return 0;
}
return change.after.ref
.update(
{
[outputField]: hash.hash,
}
);
});
यह फ़ंक्शन, Firestore ट्रिगर है. डेटाबेस में राइट इवेंट होने पर, फ़ंक्शन उस इवेंट पर प्रतिक्रिया देता है. इसके लिए, वह xv फ़ील्ड और yv फ़ील्ड खोजता है. अगर ये दोनों फ़ील्ड मौजूद होते हैं, तो वह जियोहैश का हिसाब लगाता है और आउटपुट को किसी दस्तावेज़ के आउटपुट की तय की गई जगह पर लिखता है. इनपुट दस्तावेज़ को users/{uid} कॉन्स्टेंट से तय किया जाता है. इसका मतलब है कि फ़ंक्शन, users/{uid} कलेक्शन में लिखे गए हर दस्तावेज़ को पढ़ता है. इसके बाद, उन दस्तावेज़ों के लिए जियोहैश प्रोसेस करता है.users/ इसके बाद, यह हैश को उसी दस्तावेज़ के हैश फ़ील्ड में आउटपुट करता है.
कॉल किए जा सकने वाले फ़ंक्शन
index.ts फ़ाइल में मौजूद अगला फ़ंक्शन ऐसा दिखता है:
index.ts
export const callableHash = onCall((data, context) => {
if (context.auth == undefined) {
return {error: "Only authorized users are allowed to call this endpoint"};
}
const x = data[xField];
const y = data[yField];
if (x == undefined || y == undefined) {
return {error: "Either x or y parameter was not declared"};
}
const result = service.convertToHash(x, y);
if (result.status != ResultStatusCode.ok) {
return {error: `Something went wrong ${result.message}`};
}
return {result: result.hash};
});
onCall फ़ंक्शन पर ध्यान दें. इससे पता चलता है कि यह फ़ंक्शन एक कॉल किया जा सकने वाला फ़ंक्शन है. इसे आपके क्लाइंट ऐप्लिकेशन कोड से कॉल किया जा सकता है. यह कॉल किया जा सकने वाला फ़ंक्शन, x और y पैरामीटर लेता है और एक जियोहैश दिखाता है. इस कोडलैब में, इस फ़ंक्शन को सीधे तौर पर कॉल नहीं किया जाएगा. हालांकि, इसे यहां Firebase एक्सटेंशन में कॉन्फ़िगर की जाने वाली किसी चीज़ के उदाहरण के तौर पर शामिल किया गया है.
4. extension.yaml फ़ाइल सेट अप करना
अब आपको पता चल गया है कि आपके एक्सटेंशन में मौजूद Cloud Functions कोड क्या करता है. इसलिए, अब इसे डिस्ट्रिब्यूट करने के लिए पैकेज किया जा सकता है. हर Firebase एक्सटेंशन के साथ एक extension.yaml फ़ाइल मिलती है. इसमें बताया जाता है कि एक्सटेंशन क्या करता है और यह कैसे काम करता है.
extension.yaml फ़ाइल के लिए, आपके एक्सटेंशन के बारे में कुछ शुरुआती मेटाडेटा की ज़रूरत होती है. यहां दिए गए हर चरण में, आपको यह समझने में मदद मिलेगी कि सभी फ़ील्ड का क्या मतलब है और आपको उनकी ज़रूरत क्यों है.
- उस प्रोजेक्ट की रूट डायरेक्ट्री में एक
extension.yamlफ़ाइल बनाएं जिसे आपने पहले डाउनलोड किया था. इनसे शुरुआत करें:
name: geohash-ext
version: 0.0.1
specVersion: v1beta # Firebase Extensions specification version (do not edit)
एक्सटेंशन के नाम का इस्तेमाल, एक्सटेंशन के इंस्टेंस आईडी के आधार के तौर पर किया जाता है. उपयोगकर्ता किसी एक्सटेंशन के कई इंस्टेंस इंस्टॉल कर सकते हैं. हर इंस्टेंस का अपना आईडी होता है. इसके बाद, Firebase उस इंस्टेंस आईडी का इस्तेमाल करके, एक्सटेंशन के सेवा खातों और एक्सटेंशन से जुड़े संसाधनों के नाम जनरेट करता है. वर्शन नंबर से, आपके एक्सटेंशन के वर्शन का पता चलता है. यह सिमैंटिक वर्शनिंग के मुताबिक होना चाहिए. साथ ही, एक्सटेंशन की सुविधाओं में बदलाव करने पर, आपको इसे अपडेट करना होगा. एक्सटेंशन स्पेसिफ़िकेशन के वर्शन का इस्तेमाल यह तय करने के लिए किया जाता है कि Firebase एक्सटेंशन के किस स्पेसिफ़िकेशन का पालन करना है. इस मामले में, v1beta का इस्तेमाल किया जाता है.
- YAML फ़ाइल में, इस्तेमाल में आसान जानकारी जोड़ें:
...
displayName: Latitude and longitude to GeoHash converter
description: A converter for changing your Latitude and longitude coordinates to geohashes.
डिसप्ले नेम, आपके एक्सटेंशन के नाम को आसान तरीके से दिखाता है. यह नाम, डेवलपर को तब दिखता है, जब वे आपके एक्सटेंशन के साथ इंटरैक्ट करते हैं. ब्यौरे में, एक्सटेंशन के काम करने के तरीके के बारे में कम शब्दों में जानकारी दी गई है. extensions.dev पर एक्सटेंशन डिप्लॉय करने पर, यह कुछ इस तरह दिखता है:

- अपने एक्सटेंशन में मौजूद कोड के लिए लाइसेंस की जानकारी दें.
...
license: Apache-2.0 # The license you want for the extension
- यह बताएं कि एक्सटेंशन किसने बनाया है. साथ ही, यह भी बताएं कि इसे इंस्टॉल करने के लिए बिलिंग की ज़रूरत है या नहीं:
...
author:
authorName: AUTHOR_NAME
url: https://github.com/Firebase
billingRequired: true
author सेक्शन का इस्तेमाल, उपयोगकर्ताओं को यह बताने के लिए किया जाता है कि अगर उन्हें एक्सटेंशन से जुड़ी कोई समस्या आ रही है या उन्हें इसके बारे में ज़्यादा जानकारी चाहिए, तो वे किससे संपर्क करें. billingRequired एक ज़रूरी पैरामीटर है और इसे true पर सेट किया जाना चाहिए, क्योंकि सभी एक्सटेंशन Cloud Functions पर काम करते हैं. इसके लिए, ब्लेज़ प्लान की ज़रूरत होती है.
इसमें इस एक्सटेंशन की पहचान करने के लिए, extension.yaml फ़ाइल में कम से कम ज़रूरी फ़ील्ड शामिल होते हैं. एक्सटेंशन में बताई जा सकने वाली अन्य पहचान से जुड़ी जानकारी के बारे में ज़्यादा जानने के लिए, दस्तावेज़ देखें.
5. Cloud Functions के कोड को एक्सटेंशन संसाधन में बदलना
एक्सटेंशन रिसॉर्स, एक ऐसा आइटम होता है जिसे एक्सटेंशन इंस्टॉल करने के दौरान Firebase, प्रोजेक्ट में बनाता है. इसके बाद, एक्सटेंशन के पास उन संसाधनों का मालिकाना हक होता है. साथ ही, उसके पास एक खास सेवा खाता होता है जो उन संसाधनों पर काम करता है. इस प्रोजेक्ट में, वे संसाधन Cloud Functions हैं. इन्हें extension.yaml फ़ाइल में तय किया जाना चाहिए, क्योंकि एक्सटेंशन, फ़ंक्शन फ़ोल्डर में मौजूद कोड से अपने-आप संसाधन नहीं बनाएगा. अगर आपकी Cloud Functions को संसाधन के तौर पर साफ़ तौर पर नहीं बताया गया है, तो एक्सटेंशन को डिप्लॉय करते समय उन्हें डिप्लॉय नहीं किया जा सकता.
उपयोगकर्ता की तय की गई डिप्लॉयमेंट की जगह
- उपयोगकर्ता को यह तय करने की अनुमति दें कि उसे इस एक्सटेंशन को कहां डिप्लॉय करना है. साथ ही, यह तय करने की अनुमति दें कि एक्सटेंशन को अपने असली उपयोगकर्ताओं के पास होस्ट करना बेहतर होगा या अपने डेटाबेस के पास.
extension.yamlफ़ाइल में, जगह चुनने का विकल्प शामिल करें.
extension.yaml
अब फ़ंक्शन रिसॉर्स के लिए कॉन्फ़िगरेशन लिखा जा सकता है.
extension.yamlफ़ाइल में,locationUpdateफ़ंक्शन के लिए एक संसाधन ऑब्जेक्ट बनाएं.extension.yamlफ़ाइल में यह जोड़ें:
resources:
- name: locationUpdate
type: firebaseextensions.v1beta.function
properties:
eventTrigger:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/${PROJECT_ID}/databases/(default)/documents/users/{uid}
name को प्रोजेक्ट की index.ts फ़ाइल में तय किए गए फ़ंक्शन के नाम के तौर पर तय किया जाता है. आपको डिप्लॉय किए जा रहे फ़ंक्शन का type तय करना होगा. फ़िलहाल, यह हमेशा firebaseextensions.v1beta.function होना चाहिए. इसके बाद, इस फ़ंक्शन का properties तय किया जाता है. तय की जाने वाली पहली प्रॉपर्टी, इस फ़ंक्शन से जुड़ा eventTrigger होता है. एक्सटेंशन में फ़िलहाल काम करने वाली सुविधाओं को शामिल करने के लिए, providers/cloud.firestore/eventTypes/document.write के eventType का इस्तेमाल करें. यह अपने एक्सटेंशन के लिए Cloud Functions लिखना दस्तावेज़ में मौजूद है. resource को दस्तावेज़ों की जगह के तौर पर तय किया जाता है. आपका मौजूदा लक्ष्य, कोड में मौजूद जानकारी को कॉपी करना है. इसलिए, दस्तावेज़ का पाथ users/{uid} को सुनता है. इसमें डिफ़ॉल्ट डेटाबेस की जगह पहले से मौजूद होती है.
- एक्सटेंशन को Firestore डेटाबेस के लिए, पढ़ने और लिखने की अनुमतियों की ज़रूरत होती है.
extension.yamlफ़ाइल के आखिर में, उन IAM भूमिकाओं के बारे में बताएं जिनका ऐक्सेस एक्सटेंशन के पास होना चाहिए. इससे डेवलपर के Firebase प्रोजेक्ट में डेटाबेस के साथ काम किया जा सकेगा.
roles:
- role: datastore.user
reason: Allows the extension to read / write to your Firestore instance.
datastore.user भूमिका, एक्सटेंशन के लिए इस्तेमाल की जा सकने वाली IAM भूमिकाओं की सूची से मिलती है. एक्सटेंशन को पढ़ने और लिखने की अनुमति देनी है. इसलिए, यहां datastore.user भूमिका सही रहेगी.
- कॉलेबल फ़ंक्शन को भी जोड़ना होगा.
extension.yamlफ़ाइल में, resources प्रॉपर्टी के तहत एक नया संसाधन बनाएं. ये प्रॉपर्टी, कॉल किए जा सकने वाले फ़ंक्शन के लिए खास होती हैं:
- name: callableHash
type: firebaseextensions.v1beta.function
properties:
httpsTrigger: {}
हालांकि, पिछले संसाधन में eventTrigger का इस्तेमाल किया गया था, लेकिन यहां httpsTrigger का इस्तेमाल किया गया है. इसमें कॉल किए जा सकने वाले फ़ंक्शन और एचटीटीपीएस फ़ंक्शन, दोनों शामिल हैं.
कोड की जांच
extension.yaml को आपकी index.ts फ़ाइल में मौजूद कोड से मैच करने के लिए, बहुत सारे कॉन्फ़िगरेशन करने पड़े. इस समय, पूरी हो चुकी extension.yaml फ़ाइल ऐसी दिखनी चाहिए:
extension.yaml
name: geohash-ext
version: 0.0.1
specVersion: v1beta # Firebase Extensions specification version (do not edit)
displayName: Latitude and Longitude to GeoHash converter
description: A converter for changing your Latitude and Longitude coordinates to geohashes.
license: Apache-2.0 # The license you want for the extension
author:
authorName: Sparky
url: https://github.com/Firebase
billingRequired: true
resources:
- name: locationUpdate
type: firebaseextensions.v1beta.function
properties:
eventTrigger:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/${PROJECT_ID}/databases/(default)/documents/users/{uid}
- name: callableHash
type: firebaseextensions.v1beta.function
properties:
httpsTrigger: {}
roles:
- role: datastore.user
reason: Allows the extension to read / write to your Firestore instance.
स्टेटस की जांच करना
इस समय, आपके पास एक्सटेंशन के शुरुआती फ़ंक्शनल कॉम्पोनेंट सेट अप हैं. इसलिए, Firebase एम्युलेटर का इस्तेमाल करके इसे आज़माया जा सकता है!
- अगर आपने अब तक डाउनलोड किए गए एक्सटेंशन प्रोजेक्ट के फ़ंक्शन फ़ोल्डर में
npm run buildको कॉल नहीं किया है, तो ऐसा करें. - अपने होस्ट सिस्टम पर एक नई डायरेक्ट्री बनाएं और
firebase initका इस्तेमाल करके, उस डायरेक्ट्री को अपने Firebase प्रोजेक्ट से कनेक्ट करें.
cd .. mkdir sample-proj cd sample-proj firebase init --project=projectID-or-alias
This command creates a `firebase.json` file in the directory. In the following steps, you push the configuration specified in this file to Firebase.
- उसी डायरेक्ट्री से,
firebase ext:installचलाएं./path/to/extensionको उस डायरेक्ट्री के ऐब्सलूट पाथ से बदलें जिसमें आपकीextension.yamlफ़ाइल मौजूद है.
firebase ext:install /path/to/extension
This command does two things:
- यह आपको एक्सटेंशन इंस्टेंस के लिए कॉन्फ़िगरेशन तय करने के लिए कहता है. साथ ही, यह एक
*.envफ़ाइल बनाता है. इस फ़ाइल में इंस्टेंस के लिए कॉन्फ़िगरेशन की जानकारी होती है. - इससे एक्सटेंशन इंस्टेंस, आपके
firebase.jsonकेextensionsसेक्शन में जुड़ जाता है. यह इंस्टेंस आईडी से एक्सटेंशन वर्शन तक के मैप के तौर पर काम करता है. - प्रोजेक्ट को स्थानीय तौर पर डिप्लॉय किया जा रहा है. इसलिए, यह तय किया जा सकता है कि Google Cloud Secret Manager के बजाय, किसी स्थानीय फ़ाइल का इस्तेमाल करना है.

- नए कॉन्फ़िगरेशन के साथ Firebase Emulator Suite शुरू करें:
firebase emulators:start
emulators:startचलाने के बाद, एम्युलेटर के वेबव्यू में Firestore टैब पर जाएं.usersनंबर फ़ील्ड औरyvनंबर फ़ील्ड के साथ,usersकलेक्शन में कोई दस्तावेज़ जोड़ें.xv

- एक्सटेंशन इंस्टॉल हो जाने के बाद, यह दस्तावेज़ में
hashनाम का एक नया फ़ील्ड बनाता है.

कॉन्फ़्लिक्ट से बचने के लिए, डेटा को व्यवस्थित करें
- जांच पूरी होने के बाद, एक्सटेंशन को अनइंस्टॉल करें. आपको एक्सटेंशन कोड अपडेट करना है और बाद में मौजूदा एक्सटेंशन के साथ कोई टकराव नहीं चाहिए.
एक्सटेंशन की मदद से, एक ही एक्सटेंशन के कई वर्शन एक साथ इंस्टॉल किए जा सकते हैं. इसलिए, अनइंस्टॉल करने पर यह पक्का किया जाता है कि पहले से इंस्टॉल किए गए एक्सटेंशन के साथ कोई टकराव न हो.
firebase ext:uninstall geohash-ext
मौजूदा समाधान काम करता है. हालांकि, प्रोजेक्ट की शुरुआत में बताया गया था कि सेवा से कम्यूनिकेट करने के लिए, हार्ड-कोड किया गया एपीआई पासकोड मौजूद है. मूल रूप से दी गई एपीआई पासकोड के बजाय, असली उपयोगकर्ता के एपीआई पासकोड का इस्तेमाल कैसे किया जा सकता है? इसके बारे में जानने के लिए आगे पढ़ें.
6. एक्सटेंशन को उपयोगकर्ता के हिसाब से कॉन्फ़िगर करने की सुविधा देना
कोड लैब के इस चरण में, आपके पास एक ऐसा एक्सटेंशन है जिसे उन फ़ंक्शन के लिए कॉन्फ़िगर किया गया है जिन्हें आपने पहले ही लिखा है. हालांकि, अगर आपका उपयोगकर्ता कार्टेशियन प्लेन पर जगह की जानकारी देने वाले फ़ील्ड के लिए, y और x के बजाय अक्षांश और देशांतर का इस्तेमाल करना चाहता है, तो क्या होगा? इसके अलावा, असली उपयोगकर्ता को दी गई एपीआई कुंजी का इस्तेमाल करने के बजाय, उसे अपनी एपीआई कुंजी देने के लिए कैसे कहा जा सकता है? ऐसा करने पर, उस एपीआई के लिए तय किया गया कोटा बहुत जल्द खत्म हो सकता है. इस मामले में, आपको पैरामीटर सेट अप करने और उनका इस्तेमाल करने की ज़रूरत होती है.
extension.yaml फ़ाइल में बुनियादी पैरामीटर तय करना
उन आइटम को बदलकर शुरू करें जिनके लिए डेवलपर के पास कस्टम कॉन्फ़िगरेशन हो सकता है. पहले XFIELD और YFIELD पैरामीटर होंगे.
extension.yamlफ़ाइल में, यह कोड जोड़ें. इसमेंXFIELDऔरYFIELDफ़ील्ड पैरामीटर का इस्तेमाल किया गया है. ये पैरामीटर, पहले से तय की गईparamsYAML प्रॉपर्टी में मौजूद होते हैं:
extension.yaml
params:
- param: XFIELD
label: The X Field Name
description: >-
The X Field is also known as the **longitude** value. What does
your Firestore instance refer to as the X value or the longitude
value. If no value is specified, the extension searches for
field 'xv'.
type: string
validationRegex: ^\D([0-9a-zA-Z_.]{0,375})$
validationErrorMessage: >-
The field can only contain uppercase or lowercase letter, numbers,
_, and . characters and must be less than 1500 bytes long. The field
must also not start with a number.
default: xv
required: false
immutable: false
example: xv
- param: YFIELD
label: The Y Field Name
description: >-
The Y Field is also known as the **latitude** value. What does
your Firestore instance refer to as the Y value or the latitude
value. If no value is specified, the extension searches for
field 'yv'.
type: string
validationRegex: ^\D([0-9a-zA-Z_.]{0,375})$
validationErrorMessage: >-
The field can only contain uppercase or lowercase letter, numbers,
_, and . characters and must be less than 1500 bytes long. The field
must also not start with a number.
default: yv
required: false
immutable: false
example: yv
- param, पैरामीटर का नाम इस तरह से दिखाता है कि वह आपको यानी एक्सटेंशन बनाने वाले को दिखे. पैरामीटर की वैल्यू तय करते समय, इस वैल्यू का इस्तेमाल बाद में करें.
- label एक ऐसा आइडेंटिफ़ायर है जिसे कोई भी व्यक्ति आसानी से पढ़ सकता है. इससे डेवलपर को यह पता चलता है कि पैरामीटर क्या काम करता है.
- description से वैल्यू के बारे में पूरी जानकारी मिलती है. इसमें मार्कडाउन का इस्तेमाल किया जा सकता है. इसलिए, इसे ज़्यादा दस्तावेज़ों से लिंक किया जा सकता है. इसके अलावा, इसमें ऐसे शब्दों को हाइलाइट किया जा सकता है जो डेवलपर के लिए ज़रूरी हो सकते हैं.
- type से यह तय होता है कि उपयोगकर्ता, पैरामीटर की वैल्यू को कैसे सेट करेगा. ये कई तरह के होते हैं. जैसे,
string,select,multiSelect,selectResource, औरsecret. इनमें से हर विकल्प के बारे में ज़्यादा जानने के लिए, दस्तावेज़ देखें. - validationRegex, डेवलपर की एंट्री को किसी रेगुलर एक्सप्रेशन वैल्यू तक सीमित करता है. उदाहरण में, यह फ़ील्ड के नाम से जुड़ी सामान्य गाइडलाइन पर आधारित है. यहां देखें. अगर यह काम नहीं करता है, तो...
- validationErrorMessage, डेवलपर को वैल्यू के काम न करने की सूचना देता है.
- default वह वैल्यू होती है जो डेवलपर के कोई टेक्स्ट इनपुट न करने पर दिखेगी.
- ज़रूरी नहीं है का मतलब है कि डेवलपर को कोई भी टेक्स्ट डालने की ज़रूरत नहीं है.
- immutable की मदद से डेवलपर, इस एक्सटेंशन को अपडेट कर सकता है और इस वैल्यू को बदल सकता है. इस मामले में, डेवलपर को अपनी ज़रूरतों के हिसाब से फ़ील्ड के नाम बदलने का विकल्प मिलना चाहिए.
- उदाहरण से पता चलता है कि मान्य इनपुट कैसा दिख सकता है.
यह जानकारी काफ़ी ज़्यादा है!
- खास पैरामीटर जोड़ने से पहले, आपको
extension.yamlफ़ाइल में तीन और पैरामीटर जोड़ने होंगे.
- param: INPUTPATH
label: The input document to listen to for changes
description: >-
This is the document where you write an x and y value to. Once
that document has received a value, it notifies the extension to
calculate a geohash and store that in an output document in a certain
field. This accepts function [wildcard parameters](https://firebase.google.com/docs/functions/firestore-events#wildcards-parameters)
type: string
validationRegex: ^[^/]+(/[^/]*/[^/]*)*/[^/]+$
validationErrorMessage: >-
This must point to a document path, not a collection path from the root
of the database. It must also not start or end with a '/' character.
required: true
immutable: false
example: users/{uid}
- param: OUTPUTFIELD
label: Geohash field
description: >-
This specifies the field in the output document to store the geohash in.
type: string
validationRegex: ^\D([0-9a-zA-Z_.]{0,375})$
validationErrorMessage: >-
The field can only contain uppercase or lowercase letter, numbers,
_, and . characters and must be less than 1500 bytes long. The field
must also not start with a number.
required: false
default: hash
immutable: false
example: hash
संवेदनशील पैरामीटर तय करना
अब आपको उस एपीआई पासकोड को मैनेज करना होगा जिसे उपयोगकर्ता ने डाला है. यह एक संवेदनशील स्ट्रिंग है. इसे फ़ंक्शन में सादे टेक्स्ट के तौर पर सेव नहीं किया जाना चाहिए. इसके बजाय, इस वैल्यू को Cloud Secret Manager में सेव करें. यह क्लाउड में मौजूद एक खास जगह होती है. यहां एन्क्रिप्ट (सुरक्षित) किए गए सीक्रेट सेव किए जाते हैं. साथ ही, इन्हें गलती से लीक होने से बचाया जाता है. इस सेवा का इस्तेमाल करने के लिए, डेवलपर को शुल्क चुकाना पड़ता है. हालांकि, इससे डेवलपर की एपीआई कुंजियों के लिए सुरक्षा की एक अतिरिक्त लेयर जुड़ जाती है. साथ ही, धोखाधड़ी वाली गतिविधियों को सीमित किया जा सकता है. उपयोगकर्ता के दस्तावेज़ में डेवलपर को यह सूचना दी जाती है कि यह एक सशुल्क सेवा है, ताकि बिलिंग में कोई समस्या न हो. कुल मिलाकर, इसका इस्तेमाल ऊपर बताई गई अन्य स्ट्रिंग रिसॉर्स की तरह ही किया जाता है. इनमें सिर्फ़ टाइप का अंतर है, जिसे secret कहा जाता है.
extension.yamlफ़ाइल में, यह कोड जोड़ें:
extension.yaml
- param: APIKEY
label: GeohashService API Key
description: >-
Your geohash service API Key. Since this is a demo, and not a real
service, you can use : 1234567890.
type: secret
required: true
immutable: false
resource पैरामीटर इस्तेमाल करने के लिए एट्रिब्यूट अपडेट करें
जैसा कि पहले बताया गया है, संसाधन (फ़ंक्शन नहीं) यह तय करता है कि संसाधन को कैसे देखा जाए. इसलिए, नए पैरामीटर का इस्तेमाल करने के लिए, locationUpdate संसाधन को अपडेट करना होगा.
extension.yamlफ़ाइल में, यह कोड जोड़ें:
extension.yaml
## Change from this
- name: locationUpdate
type: firebaseextensions.v1beta.function
properties:
eventTrigger:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/${PROJECT_ID}/databases/(default)/documents/users/{uid}]
## To this
- name: locationUpdate
type: firebaseextensions.v1beta.function
properties:
eventTrigger:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/${PROJECT_ID}/databases/(default)/documents/${INPUTPATH}
extension.yaml फ़ाइल देखें
extension.yamlफ़ाइल की समीक्षा करें. यह कुछ ऐसी नज़र आनी चाहिए:
extension.yaml
name: geohash-ext
version: 0.0.1
specVersion: v1beta # Firebase Extensions specification version (do not edit)
displayName: Latitude and Longitude to GeoHash converter
description: A converter for changing your Latitude and Longitude coordinates to geohashes.
license: Apache-2.0 # The license you want to use for the extension
author:
authorName: Sparky
url: https://github.com/Firebase
billingRequired: true
params:
- param: XFIELD
label: The X Field Name
description: >-
The X Field is also known as the **longitude** value. What does
your Firestore instance refer to as the X value or the longitude
value. If you don't provide a value for this field, the extension will use 'xv' as the default value.
type: string
validationRegex: ^\D([0-9a-zA-Z_.]{0,375})$
validationErrorMessage: >-
The field can only contain uppercase or lowercase letter, numbers,
_, and . characters and must be less than 1500 bytes long. The field
must also not start with a number.
default: xv
required: false
immutable: false
example: xv
- param: YFIELD
label: The Y Field Name
description: >-
The Y Field is also known as the **latitude** value. What does
your Firestore instance refer to as the Y value or the latitude
Value. If you don't provide a value for this field, the extension will use 'yv' as the default value.
type: string
validationRegex: ^\D([0-9a-zA-Z_.]{0,375})$
validationErrorMessage: >-
The field can only contain uppercase or lowercase letter, numbers,
_, and . characters and must be less than 1500 bytes long. The field
must also not start with a number.
default: yv
required: false
immutable: false
example: yv
- param: INPUTPATH
label: The input document to listen to for changes
description: >-
This is the document where you write an x and y value to. Once
that document has been modified, it notifies the extension to
compute a geohash and store that in an output document in a certain
field. This accepts function [wildcard parameters](https://firebase.google.com/docs/functions/firestore-events#wildcards-parameters)
type: string
validationRegex: ^[^/]+(/[^/]*/[^/]*)*/[^/]+$
validationErrorMessage: >-
This must point to a document path, not a collection path from the root
of the database. It must also not start or end with a '/' character.
required: true
immutable: false
example: users/{uid}
- param: OUTPUTFIELD
label: Geohash field
description: >-
This specifies the field in the output document to store the geohash in.
type: string
validationRegex: ^\D([0-9a-zA-Z_.]{0,375})$
validationErrorMessage: >-
The field can only contain uppercase or lowercase letter, numbers,
_, and . characters and must be less than 1500 bytes long. The field
must also not start with a number.
required: false
default: hash
immutable: false
example: hash
- param: APIKEY
label: GeohashService API Key
description: >-
Your geohash service API Key. Since this is a demo, and not a real
service, you can use : 1234567890.
type: secret
required: true
immutable: false
resources:
- name: locationUpdate
type: firebaseextensions.v1beta.function
properties:
eventTrigger:
eventType: providers/cloud.firestore/eventTypes/document.write
resource: projects/${PROJECT_ID}/databases/(default)/documents/${INPUTPATH}
- name: callableHash
type: firebaseextensions.v1beta.function
properties:
httpsTrigger: {}
roles:
- role: datastore.user
reason: Allows the extension to read / write to your Firestore instance.
कोड में पैरामीटर ऐक्सेस करना
अब जब सभी पैरामीटर extension.yaml फ़ाइल में कॉन्फ़िगर हो गए हैं, तो उन्हें index.ts फ़ाइल में जोड़ें.
index.tsफ़ाइल में, डिफ़ॉल्ट वैल्यू कोprocess.env.PARAMETER_NAMEसे बदलें. इससे सही पैरामीटर वैल्यू फ़ेच होती हैं और डेवलपर के Firebase प्रोजेक्ट पर डिप्लॉय किए गए फ़ंक्शन कोड में उन्हें भरा जाता है.
index.ts
// Replace this:
const documentPath = "users/{uid}";
const xField = "xv";
const yField = "yv";
const apiKey = "1234567890";
const outputField = "hash";
// with this:
const documentPath = process.env.INPUTPATH!; // this value is ignored since its read from the resource
const xField = process.env.XFIELD!;
const yField = process.env.YFIELD!;
const apiKey = process.env.APIKEY!;
const outputField = process.env.OUTPUTFIELD!;
आम तौर पर, एनवायरमेंट वैरिएबल की वैल्यू के साथ नल चेक किए जाते हैं. हालांकि, इस मामले में आपको भरोसा है कि पैरामीटर की वैल्यू सही तरीके से कॉपी की गई हैं. अब कोड को एक्सटेंशन पैरामीटर के साथ काम करने के लिए कॉन्फ़िगर किया गया है.
7. उपयोगकर्ता के लिए दस्तावेज़ बनाना
एम्युलेटर पर या Firebase एक्सटेंशन मार्केटप्लेस में कोड की जांच करने से पहले, एक्सटेंशन का दस्तावेज़ तैयार करना ज़रूरी है. इससे डेवलपर को पता चलेगा कि एक्सटेंशन का इस्तेमाल करने पर उन्हें क्या मिलेगा.
- सबसे पहले,
PREINSTALL.mdफ़ाइल बनाएं. इसका इस्तेमाल, फ़ंक्शन के बारे में बताने, इंस्टॉल करने की ज़रूरी शर्तों के बारे में बताने, और बिलिंग से जुड़े संभावित असर के बारे में बताने के लिए किया जाता है.
PREINSTALL.md
Use this extension to automatically convert documents with a latitude and
longitude to a geohash in your database. Additionally, this extension includes a callable function that allows users to make one-time calls
to convert an x,y coordinate into a geohash.
Geohashing is supported for latitudes between 90 and -90 and longitudes
between 180 and -180.
#### Third Party API Key
This extension uses a fictitious third-party API for calculating the
geohash. You need to supply your own API keys. (Since it's fictitious,
you can use 1234567890 as an API key).
#### Additional setup
Before installing this extension, make sure that you've [set up a Cloud
Firestore database](https://firebase.google.com/docs/firestore/quickstart) in your Firebase project.
After installing this extension, you'll need to:
- Update your client code to point to the callable geohash function if you
want to perform arbitrary geohashes.
Detailed information for these post-installation tasks are provided after
you install this extension.
#### Billing
To install an extension, your project must be on the [Blaze (pay as you
go) plan](https://firebase.google.com/pricing)
- This extension uses other Firebase and Google Cloud Platform services,
which have associated charges if you exceed the service's no-cost tier:
- Cloud Firestore
- Cloud Functions (Node.js 16+ runtime. [See
FAQs](https://firebase.google.com/support/faq#extensions-pricing))
- [Cloud Secret Manager](https://cloud.google.com/secret-manager/pricing)
- इस प्रोजेक्ट के लिए
README.mdलिखने में लगने वाला समय बचाने के लिए, इस आसान तरीके का इस्तेमाल करें:
firebase ext:info . --markdown > README.md
इसमें आपकी PREINSTALL.md फ़ाइल का कॉन्टेंट और आपकी extension.yaml फ़ाइल से एक्सटेंशन के बारे में अतिरिक्त जानकारी शामिल होती है.
आखिर में, डेवलपर को अभी इंस्टॉल किए गए एक्सटेंशन के बारे में कुछ और जानकारी दें. इंस्टॉल करने के बाद, डेवलपर को कुछ अतिरिक्त निर्देश और जानकारी मिल सकती है. साथ ही, इंस्टॉल करने के बाद कुछ ज़्यादा जानकारी वाले टास्क मिल सकते हैं. जैसे, यहां क्लाइंट कोड सेट अप करना.
POSTINSTALL.mdफ़ाइल बनाएं. इसके बाद, इंस्टॉलेशन के बाद की यह जानकारी शामिल करें:
POSTINSTALL.md
Congratulations on installing the geohash extension!
#### Function information
* **Firestore Trigger** - ${function:locationUpdate.name} was installed
and is invoked when both an x field (${param:XFIELD}) and y field
(${param:YFIELD}) contain a value.
* **Callable Trigger** - ${function:callableHash.name} was installed and
can be invoked by writing the following client code:
```javascript
import { getFunctions, httpsCallable } from "firebase/functions";
const functions = getFunctions();
const geoHash = httpsCallable(functions, '${function:callableHash.name}');
geoHash({ ${param:XFIELD}: -122.0840, ${param:YFIELD}: 37.4221 })
.then((result) => {
// Read result of the Cloud Function.
/** @type {any} */
const data = result.data;
const error = data.error;
if (error != null) {
console.error(`callable error : ${error}`);
}
const result = data.result;
console.log(result);
});
मॉनिटर करने के लिए
सबसे सही तरीका यह है कि इंस्टॉल किए गए एक्सटेंशन की गतिविधि को मॉनिटर करें. इसमें, उसकी परफ़ॉर्मेंस, इस्तेमाल, और लॉग की जांच करना शामिल है.
The output rendering looks something like this when it's deployed:
<img src="img/82b54a5c6ca34b3c.png" alt="A preview of the latitude and longitude geohash converter extension in the firebase console" width="957.00" />
## Test the extension with the full configuration
Duration: 03:00
It's time to make sure that the user-configurable extension is working the way it is intended.
* Change into the functions folder and ensure that the latest compiled version of the extensions exists. In the extensions project functions directory, call:
```console
npm run build
इससे फ़ंक्शन फिर से कंपाइल हो जाते हैं, ताकि एक्सटेंशन के साथ डिप्लॉय करने के लिए, नया सोर्स कोड तैयार हो जाए. ऐसा तब होता है, जब एक्सटेंशन को सीधे तौर पर एम्युलेटर या Firebase पर डिप्लॉय किया जाता है.
इसके बाद, एक्सटेंशन की जांच करने के लिए एक नई डायरेक्ट्री बनाएं. एक्सटेंशन को मौजूदा फ़ंक्शन से डेवलप किया गया था. इसलिए, उस फ़ोल्डर से टेस्ट न करें जिसमें एक्सटेंशन को कॉन्फ़िगर किया गया था. ऐसा इसलिए, क्योंकि इससे फ़ंक्शन और Firebase के नियमों को भी डिप्लॉय करने की कोशिश की जाती है.
Firebase एम्युलेटर की मदद से इंस्टॉल करना और जांच करना
- अपने होस्ट सिस्टम पर एक नई डायरेक्ट्री बनाएं और
firebase initका इस्तेमाल करके, उस डायरेक्ट्री को अपने Firebase प्रोजेक्ट से कनेक्ट करें.
mkdir sample-proj cd sample-proj firebase init --project=projectID-or-alias
- एक्सटेंशन इंस्टॉल करने के लिए, उस डायरेक्ट्री से
firebase ext:installचलाएं./path/to/extensionकी जगह पर, उस डायरेक्ट्री का ऐब्सलूट पाथ डालें जिसमें आपकीextension.yamlफ़ाइल मौजूद है. इससे आपके एक्सटेंशन को इंस्टॉल करने की प्रोसेस शुरू हो जाती है. साथ ही, एक.envफ़ाइल बन जाती है. इसमें आपके कॉन्फ़िगरेशन होते हैं. इसके बाद, कॉन्फ़िगरेशन को Firebase या एम्युलेटर पर पुश किया जाता है.
firebase ext:install /path/to/extension
- प्रोजेक्ट को स्थानीय तौर पर डिप्लॉय किया जा रहा है. इसलिए, यह बताएं कि आपको Google Cloud Secret Manager के बजाय किसी स्थानीय फ़ाइल का इस्तेमाल करना है.

- Local Emulator Suite शुरू करें:
firebase emulators:start
किसी असली Firebase प्रोजेक्ट के साथ इंस्टॉल और टेस्ट करना
एक्सटेंशन को किसी Firebase प्रोजेक्ट में इंस्टॉल किया जा सकता है. हमारा सुझाव है कि टेस्टिंग के लिए, टेस्ट प्रोजेक्ट का इस्तेमाल करें. अगर आपको अपने एक्सटेंशन के एंड-टू-एंड फ़्लो की जांच करनी है या आपके एक्सटेंशन के ट्रिगर को Firebase Emulator Suite में अब तक काम करने की सुविधा नहीं मिली है, तो इस टेस्टिंग वर्कफ़्लो का इस्तेमाल करें. इसके बारे में जानने के लिए, एक्सटेंशन एम्युलेटर का विकल्प देखें. फ़िलहाल, एम्युलेटर इन सेवाओं के लिए, एचटीटीपी अनुरोध से ट्रिगर होने वाले फ़ंक्शन और बैकग्राउंड इवेंट से ट्रिगर होने वाले फ़ंक्शन के साथ काम करते हैं: Cloud Firestore, Realtime Database, और Pub/Sub.
- अपने होस्ट सिस्टम पर एक नई डायरेक्ट्री बनाएं और
firebase initका इस्तेमाल करके, उस डायरेक्ट्री को अपने Firebase प्रोजेक्ट से कनेक्ट करें.
cd .. mkdir sample-proj cd sample-proj firebase init --project=projectID-or-alias
- इसके बाद, एक्सटेंशन इंस्टॉल करने के लिए उस डायरेक्ट्री से
firebase ext:installचलाएं./path/to/extensionकी जगह पर, उस डायरेक्ट्री का ऐब्सलूट पाथ डालें जिसमें आपकीextension.yamlफ़ाइल मौजूद है. इससे आपके एक्सटेंशन को इंस्टॉल करने की प्रोसेस शुरू हो जाती है. साथ ही, एक.envफ़ाइल बन जाती है. इसमें आपके कॉन्फ़िगरेशन होते हैं. इसके बाद, कॉन्फ़िगरेशन को Firebase या एम्युलेटर पर पुश किया जाता है.
firebase ext:install /path/to/extension
- अगर आपको सीधे तौर पर Firebase पर डिप्लॉय करना है और Google Cloud Secret Manager का इस्तेमाल करना है, तो एक्सटेंशन इंस्टॉल करने से पहले, आपको Secret Manager API चालू करना होगा.
- अपने Firebase प्रोजेक्ट में डिप्लॉय करें.
firebase deploy
एक्सटेंशन को आज़माना
firebase deployयाfirebase emulators:startचलाने के बाद, Firebase कंसोल या एम्युलेटर के वेबव्यू के Firestore टैब पर जाएं.xफ़ील्ड औरyफ़ील्ड में दिए गए कलेक्शन में कोई दस्तावेज़ जोड़ें. इस मामले में, अपडेट किए गए दस्तावेज़u/{uid}पर मौजूद हैं. इनमेंxफ़ील्ड की वैल्यूxvऔरyफ़ील्ड की वैल्यूyvहै.

- अगर आपने एक्सटेंशन इंस्टॉल कर लिया है, तो एक्सटेंशन, दोनों फ़ील्ड सेव करने के बाद दस्तावेज़ में
hashनाम का एक नया फ़ील्ड बनाता है.

8. बधाई हो!
आपने अपनी पहली Cloud फ़ंक्शन को Firebase एक्सटेंशन में बदल दिया है!
आपने extension.yaml फ़ाइल जोड़ी है और उसे कॉन्फ़िगर किया है, ताकि डेवलपर यह चुन सकें कि वे आपके एक्सटेंशन को कैसे डिप्लॉय करना चाहते हैं. इसके बाद, आपने उपयोगकर्ता के लिए दस्तावेज़ बनाया. इसमें एक्सटेंशन सेट अप करने से पहले, एक्सटेंशन के डेवलपर को क्या करना चाहिए, इसके बारे में दिशा-निर्देश दिए गए हैं. साथ ही, एक्सटेंशन को इंस्टॉल करने के बाद, उन्हें कौनसे चरण पूरे करने पड़ सकते हैं, इसके बारे में भी बताया गया है.
अब आपको Firebase फ़ंक्शन को डिस्ट्रिब्यूट किए जा सकने वाले Firebase एक्सटेंशन में बदलने के लिए ज़रूरी मुख्य चरणों के बारे में पता चल गया है.