@@ -34,6 +34,7 @@ import {entity} from './entity';
3434import { Query } from './query' ;
3535import { DatastoreRequest } from './request' ;
3636import { Transaction } from './transaction' ;
37+ import { promisifyAll } from '@google-cloud/promisify' ;
3738
3839const { grpc} = new GrpcClient ( ) ;
3940
@@ -730,6 +731,16 @@ class Datastore extends DatastoreRequest {
730731 return Datastore . isKey ( value ) ;
731732 }
732733
734+ keyToLegacyUrlSafe ( key : entity . Key , locationPrefix ?: string ) : Promise < string > ;
735+ keyToLegacyUrlSafe (
736+ key : entity . Key ,
737+ callback : KeyToLegacyUrlSafeCallback
738+ ) : void ;
739+ keyToLegacyUrlSafe (
740+ key : entity . Key ,
741+ locationPrefix : string ,
742+ callback : KeyToLegacyUrlSafeCallback
743+ ) : void ;
733744 /**
734745 * Helper to create a URL safe key.
735746 *
@@ -744,26 +755,62 @@ class Datastore extends DatastoreRequest {
744755 * The location prefix of an App Engine project ID.
745756 * Often this value is 's~', but may also be 'e~', or other location prefixes
746757 * currently unknown.
747- * @returns {string } base64 endocded urlsafe key.
758+ * @param {function } callback The callback function.
759+ * @param {?error } callback.err An error returned while making this request
760+ * @param {string } callback.urlSafeKey A Base64-encoded URL-safe key.
748761 *
749762 * @example
750763 * const {Datastore} = require('@google-cloud/datastore');
751764 * const datastore = new Datastore();
752765 * const key = datastore.key(['Company', 'Google']);
753766 *
754- * datastore.keyToLegacyUrlsafe(key) //ag9ncmFzcy1jbHVtcC00NzlyEwsSB0NvbXBhbnkiBkdvb2dsZQw
767+ * datastore.keyToLegacyUrlSafe(key, (err, urlSafeKey) => {
768+ * if (err) {
769+ * // Error handling omitted.
770+ * }
771+ * console.log(urlSafeKey);
772+ * });
755773 *
756- * @example
757- * <caption>Create a complete url safe key using location prefix </caption>
758- * const {Datastore} = require('@google-cloud/datastore');
759- * const datastore = new Datastore();
760- * const key = datastore.key(['Task', 123]);
774+ * //-
775+ * // Create a complete URL-safe key using a location prefix.
776+ * //-
761777 * const locationPrefix = 's~';
762778 *
763- * datastore.keyToLegacyUrlsafe(key, locationPrefix) //ahFzfmdyYXNzLWNsdW1wLTQ3OXIKCxIEVGFzaxh7DA
779+ * datastore.keyToLegacyUrlSafe(key, locationPrefix, (err, urlSafeKey) => {
780+ * if (err) {
781+ * // Error handling omitted.
782+ * }
783+ * console.log(urlSafeKey);
784+ * });
785+ *
786+ * //-
787+ * // If the callback is omitted, we'll return a Promise.
788+ * //-
789+ * datastore.keyToLegacyUrlSafe(key).then((data) => {
790+ * const urlSafeKey = data[0];
791+ * console.log(urlSafeKey);
792+ * });
764793 */
765- keyToLegacyUrlsafe ( key : entity . Key , locationPrefix ?: string ) : string {
766- return urlSafeKey . legacyEncode ( this . projectId , key , locationPrefix ) ;
794+ keyToLegacyUrlSafe (
795+ key : entity . Key ,
796+ locationPrefixOrCallback ?: string | KeyToLegacyUrlSafeCallback ,
797+ callback ?: KeyToLegacyUrlSafeCallback
798+ ) : Promise < string > | void {
799+ const locationPrefix =
800+ typeof locationPrefixOrCallback === 'string'
801+ ? locationPrefixOrCallback
802+ : '' ;
803+ callback =
804+ typeof locationPrefixOrCallback === 'function'
805+ ? locationPrefixOrCallback
806+ : callback ;
807+ this . auth . getProjectId ( ( err , projectId ) => {
808+ if ( err ) {
809+ callback ! ( err ) ;
810+ return ;
811+ }
812+ callback ! ( null , urlSafeKey . legacyEncode ( projectId ! , key , locationPrefix ) ) ;
813+ } ) ;
767814 }
768815
769816 /**
@@ -870,6 +917,27 @@ class Datastore extends DatastoreRequest {
870917 Transaction = Transaction ;
871918}
872919
920+ /*! Developer Documentation
921+ *
922+ * All async methods (except for streams) will return a Promise in the event
923+ * that a callback is omitted.
924+ */
925+ promisifyAll ( Datastore , {
926+ exclude : [
927+ 'double' ,
928+ 'isDouble' ,
929+ 'geoPoint' ,
930+ 'isGeoPoint' ,
931+ 'int' ,
932+ 'isInt' ,
933+ 'createQuery' ,
934+ 'key' ,
935+ 'isKey' ,
936+ 'keyFromLegacyUrlsafe' ,
937+ 'transaction' ,
938+ ] ,
939+ } ) ;
940+
873941export { Datastore } ;
874942
875943/**
@@ -932,3 +1000,7 @@ export interface DatastoreOptions extends GoogleAuthOptions {
9321000 apiEndpoint ?: string ;
9331001 sslCreds ?: ChannelCredentials ;
9341002}
1003+
1004+ export interface KeyToLegacyUrlSafeCallback {
1005+ ( err ?: Error | null , urlSafeKey ?: string ) : void ;
1006+ }
0 commit comments